MySQL 8.4 Release Notes
5.3.4.3 选择特定列
如果您不想看到整个表的行,只需列出您感兴趣的列,使用逗号分隔。例如,如果您想知道动物出生日期,选择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 |
+----------+------------+
要找到宠物所有者的信息,请使用以下查询:
mysql> SELECT owner FROM pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| Harold |
| Benny |
| Diane |
| Gwen |
| Gwen |
| Benny |
| Diane |
+--------+
注意,这个查询只是从每个记录中检索owner
列,并且一些记录出现多次。为了减少输出结果,可以在输出结果中添加关键字DISTINCT
以确保每个唯一输出记录只出现一次:
mysql> SELECT DISTINCT owner FROM pet;
+--------+
| owner |
+--------+
| Benny |
| Diane |
| Gwen |
| Harold |
+--------+
您可以使用WHERE
子句将行选择与列选择组合起来。例如,要获取只包含狗和猫的出生日期,请使用以下查询:
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 |
+--------+---------+------------+