If you do not want to see entire rows from your table, just name the columns in which you are interested, separated by commas. 如果不想看到表中的整行,只需命名感兴趣的列,并用逗号分隔。For example, if you want to know when your animals were born, select the 例如,如果要知道动物的出生时间,请选择name
and birth
columns:name
和birth
列:
mysql> SELECT name, birth FROM pet;
+----------+------------+
| name | birth |
+----------+------------+
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| Fang | 1990-08-27 |
| Bowser | 1989-08-31 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Puffball | 1999-03-30 |
+----------+------------+
To find out who owns pets, use this query:要查找谁拥有宠物,请使用以下查询:
mysql> SELECT owner FROM pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| Harold |
| Benny |
| Diane |
| Gwen |
| Gwen |
| Benny |
| Diane |
+--------+
Notice that the query simply retrieves the 请注意,查询只是从每个记录检索owner
column from each record, and some of them appear more than once. owner
列,其中一些记录会多次出现。To minimize the output, retrieve each unique output record just once by adding the keyword 要最小化输出,只需通过添加关键字DISTINCT
:DISTINCT
来检索每个唯一的输出记录一次:
mysql> SELECT DISTINCT owner FROM pet;
+--------+
| owner |
+--------+
| Benny |
| Diane |
| Gwen |
| Harold |
+--------+
You can use a 可以使用WHERE
clause to combine row selection with column selection. WHERE
子句将行选择与列选择相结合。For example, to get birth dates for dogs and cats only, use this query:例如,要仅获取猫和狗的出生日期,请使用以下查询:
mysql>SELECT name, species, birth FROM pet
WHERE species = 'dog' OR species = 'cat';
+--------+---------+------------+ | name | species | birth | +--------+---------+------------+ | Fluffy | cat | 1993-02-04 | | Claws | cat | 1994-03-17 | | Buffy | dog | 1989-05-13 | | Fang | dog | 1990-08-27 | | Bowser | dog | 1989-08-31 | +--------+---------+------------+