1.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-30 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
| Puffball | 1999-03-30 |
+----------------------+------------+
9 rows in set (0.00 sec)
如果您只想看到宠物的 owner :
mysql> SELECT owner FROM pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| Harold |
| Benny |
| Diane |
| Gwen |
| Gwen |
| Benny |
| Diane |
+--------+
9 rows in set (0.01 sec)
上面的例子中 owner 重复出现多个,如果您仅想结果中出现一次,那么您可以使用 DISTINCT 关键字。
mysql> SELECT DISTINCT owner FROM pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| Benny |
| Diane |
+--------+
4 rows in set (0.00 sec)
使用 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-30 |
+----------------------+---------+------------+
5 rows in set (0.00 sec)