3.2 Entering Queries输入查询

Make sure that you are connected to the server, as discussed in the previous section. 确保已连接到服务器,如前一节所述。Doing so does not in itself select any database to work with, but that is okay. 这样做本身不会选择任何要使用的数据库,但这没关系。At this point, it is more important to find out a little about how to issue queries than to jump right in creating tables, loading data into them, and retrieving data from them. 此时,了解一点如何发出查询比直接创建表、将数据加载到表中以及从表中检索数据更重要。This section describes the basic principles of entering queries, using several queries you can try out to familiarize yourself with how mysql works.本节介绍输入查询的基本原则,使用几个查询您可以尝试熟悉mysql的工作方式。

Here is a simple query that asks the server to tell you its version number and the current date. 下面是一个简单的查询,要求服务器告诉您其版本号和当前日期。Type it in as shown here following the mysql> prompt and press Enter:按如下所示输入mysql>提示并按Enter键:

mysql> SELECT VERSION(), CURRENT_DATE;
+-----------+--------------+
| VERSION() | CURRENT_DATE |
+-----------+--------------+
| 5.8.0-m17 | 2015-12-21   |
+-----------+--------------+
1 row in set (0.02 sec)
mysql>

This query illustrates several things about mysql:此查询说明了mysql的几个方面:

Keywords may be entered in any lettercase. The following queries are equivalent:关键字可以用任何字母输入。以下查询是等效的:

mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;

Here is another query. 这是另一个问题。It demonstrates that you can use mysql as a simple calculator:它演示了您可以将mysql用作一个简单的计算器:

mysql> SELECT SIN(PI()/4), (4+1)*5;
+------------------+---------+
| SIN(PI()/4)      | (4+1)*5 |
+------------------+---------+
| 0.70710678118655 |      25 |
+------------------+---------+
1 row in set (0.02 sec)

The queries shown thus far have been relatively short, single-line statements. 迄今为止显示的查询都是相对较短的单行语句。You can even enter multiple statements on a single line. Just end each one with a semicolon:您甚至可以在一行中输入多条语句。只需以分号结尾:

mysql> SELECT VERSION(); SELECT NOW();
+-----------+
| VERSION() |
+-----------+
| 8.0.13    |
+-----------+
1 row in set (0.00 sec)

+---------------------+
| NOW()               |
+---------------------+
| 2018-08-24 00:56:40 |
+---------------------+
1 row in set (0.00 sec)

A query need not be given all on a single line, so lengthy queries that require several lines are not a problem. 一个查询不需要在一行中给出全部内容,因此需要几行的冗长查询不是问题。mysql determines where your statement ends by looking for the terminating semicolon, not by looking for the end of the input line. mysql通过查找终止分号而不是输入行的结尾来确定语句的结尾。(In other words, mysql accepts free-format input: it collects input lines but does not execute them until it sees the semicolon.)(换句话说,mysql接受自由格式输入:它收集输入行,但在看到分号之前不会执行它们。)

Here is a simple multiple-line statement:下面是一个简单的多行语句:

mysql> SELECT
    -> USER()
    -> ,
    -> CURRENT_DATE;
+---------------+--------------+
| USER()        | CURRENT_DATE |
+---------------+--------------+
| jon@localhost | 2018-08-24   |
+---------------+--------------+

In this example, notice how the prompt changes from mysql> to -> after you enter the first line of a multiple-line query. 在本例中,请注意输入多行查询的第一行后,提示是如何从mysql>更改至->This is how mysql indicates that it has not yet seen a complete statement and is waiting for the rest. mysql就是这样表示它还没有看到完整的语句,正在等待其余的语句。The prompt is your friend, because it provides valuable feedback. 提示是您的朋友,因为它提供了有价值的反馈。If you use that feedback, you can always be aware of what mysql is waiting for.如果您使用这些反馈,您可以随时知道mysql在等待什么。

If you decide you do not want to execute a query that you are in the process of entering, cancel it by typing \c:如果您决定不执行正在输入的查询,请键入\c

mysql> SELECT
    -> USER()
    -> \c
mysql>

Here, too, notice the prompt. 这里也请注意提示。It switches back to mysql> after you type \c, providing feedback to indicate that mysql is ready for a new query.键入\c后它切换回mysql>,提供反馈以指示mysql已准备好进行新查询。

The following table shows each of the prompts you may see and summarizes what they mean about the state that mysql is in.下表显示了您可能看到的每个提示,并总结了它们对mysql所处状态的含义。

Prompt提示Meaning含义
mysql>Ready for new query准备好接受新的查询了吗
->Waiting for next line of multiple-line query正在等待多行查询的下一行
'>Waiting for next line, waiting for completion of a string that began with a single quote (')等待下一行,等待以单引(')号开始的字符串完成
">Waiting for next line, waiting for completion of a string that began with a double quote (")等待下一行,等待以双引号(")开头的字符串完成
`>Waiting for next line, waiting for completion of an identifier that began with a backtick (`)等待下一行,等待以倒勾(`)开头的标识符完成
/*>Waiting for next line, waiting for completion of a comment that began with /*等待下一行,等待以/*开始的注释完成

Multiple-line statements commonly occur by accident when you intend to issue a query on a single line, but forget the terminating semicolon. 当您打算在一行上发出查询,但忘记了终止分号时,通常会意外地出现多行语句。In this case, mysql waits for more input:在这种情况下,mysql将等待更多输入:

mysql> SELECT USER()
    ->

If this happens to you (you think you've entered a statement but the only response is a -> prompt), most likely mysql is waiting for the semicolon. 如果这种情况发生在您身上(您认为您输入了一条语句,但唯一的回答是->提示符),很可能mysql正在等待分号。If you don't notice what the prompt is telling you, you might sit there for a while before realizing what you need to do. 如果您没有注意到提示告诉您的内容,您可能会在意识到需要做什么之前在那里坐一会儿。Enter a semicolon to complete the statement, and mysql executes it:输入分号以完成语句,mysql将执行该语句:

mysql> SELECT USER()
    -> ;
+---------------+
| USER()        |
+---------------+
| jon@localhost |
+---------------+

The '> and "> prompts occur during string collection (another way of saying that MySQL is waiting for completion of a string). '>">在字符串收集过程中会出现提示(表示MySQL正在等待字符串完成的另一种方式)。In MySQL, you can write strings surrounded by either ' or " characters (for example, 'hello' or "goodbye"), and mysql lets you enter strings that span multiple lines. 在MySQL中,您可以编写由'"字符包围的字符串(例如,'hello'"goodbye"),mysql允许您输入跨多行的字符串。When you see a '> or "> prompt, it means that you have entered a line containing a string that begins with a ' or " quote character, but have not yet entered the matching quote that terminates the string. 当您看到一个'>">提示时,表示您已输入一行,其中包含以'"引号字符开头的字符串,但尚未输入终止该字符串的匹配引号。This often indicates that you have inadvertently left out a quote character. 这通常表示您无意中遗漏了引号字符。For example:例如:

mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
    '>

If you enter this SELECT statement, then press Enter and wait for the result, nothing happens. 如果输入此SELECT语句,然后按Enter键并等待结果,则不会发生任何事情。Instead of wondering why this query takes so long, notice the clue provided by the '> prompt. 请注意'>提示提供的线索,而不是疑惑此查询为何需要如此长的时间。It tells you that mysql expects to see the rest of an unterminated string. 它告诉您mysql希望看到未终止字符串的其余部分。(Do you see the error in the statement? The string 'Smith is missing the second single quotation mark.)(你看到语句中的错误了吗?字符串'Smith缺少第二个单引号。)

At this point, what do you do? 在这一点上,你会怎么做?The simplest thing is to cancel the query. 最简单的方法是取消查询。However, you cannot just type \c in this case, because mysql interprets it as part of the string that it is collecting. 但是,在本例中不能只键入\c,因为mysql将其解释为它正在收集的字符串的一部分。Instead, enter the closing quote character (so mysql knows you've finished the string), then type \c:相反,请输入结束引号字符(以便mysql知道您已经完成了字符串),然后键入\c:

mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
    '> '\c
mysql>

The prompt changes back to mysql>, indicating that mysql is ready for a new query.提示变回mysql>,表示mysql已准备好进行新查询。

The `> prompt is similar to the '> and "> prompts, but indicates that you have begun but not completed a backtick-quoted identifier.`>提示类似于'>">提示,但表示您已开始但未完成反勾引号标识符。

It is important to know what the '>, ">, and `> prompts signify, because if you mistakenly enter an unterminated string, any further lines you type appear to be ignored by mysql—including a line containing QUIT. 了解'>">`>提示意味着什么很重要,因为如果您错误地输入了一个未终止的字符串,mysql会忽略您输入的任何其他行,包括包含QUIT的行。This can be quite confusing, especially if you do not know that you need to supply the terminating quote before you can cancel the current query.这可能会非常混乱,尤其是如果您不知道在取消当前查询之前需要提供终止报价。

Note注意

Multiline statements from this point on are written without the secondary (-> or other) prompts, to make it easier to copy and paste the statements to try for yourself.从这一点开始,多行语句在编写时不使用辅助(->或其他)提示,以便更轻松地复制和粘贴语句,以便自己尝试。