从表检索信息
在前面,我, 创建了一个数据库test 进入数据库 创建了一个表pet 插入了一些数据。那么问题来了,我要是想要瞅一瞅我的表中的数据肿么办呢?莫着急,待我一一道来
SELECT语句
语句的一般格式:
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
这是一个简单的SELECT语句的基本格式了,详细复杂的以后再说。
选择所有数据
SELECT最简单的形式是从一个表中检索所有记录:
mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+
9 rows in set (0.00 sec)
mysql>
没错!就是有这种操作,厉不厉害。(?????)
简单讲一讲(其实没讲)UPDATE语句
现在能看见表中的所有数据了,你就何以检查表中的数据有没有错误了。
这时候你突然发现(书上提示的),Bowser的出生日期不对呀,应该是1989年出生的,而不是1979年。
So,你就想怎么更改这条数据。于是你在某一个月光下的夜晚,你悄悄的删除(DELETE)了表中的所有数据,又在pet.txt文件中更改了回来,重新将pet.txt导入(LOAD DATA)到表中。这样做显然是非常愚蠢的。
然而,在很久很久以前上神们曾经创造过这样的一个命令 UPDATE 用于更改有问题的记录用的。
mysql> UPDATE pet SET birth = ‘1989-08-31‘ WHERE name = ‘Bowser‘;
Query OK, 1 row affected (0.18 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
这样就更改了错误的数据。
选择特殊行
举个栗子:刚才我用上神赐予我的UPDATE语句,更改了Bowser的生日,我想看一看有木有更改成功。
操作如下:
SELECT * FROM pet WHERE name = ‘Bowser‘;
mysql> SELECT *
-> FROM pet
-> WHERE name = ‘Bowser‘;
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
1 row in set (0.00 sec)
mysql>
嗯。。。看来上神没有骗我们
接着举个例子:我想要知道哪个动物在1998年以后出生的
操作如下:
SELECT * FROM pet WHERE birth > ‘1998-1-1‘;
mysql> SELECT *
-> FROM pet
-> WHERE birth > ‘1998-01-01‘;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
+----------+-------+---------+------+------------+-------+
2 rows in set (0.14 sec)
mysql>
接着举个栗子:我想要给我们家李狗蛋找一个小母狗交配,我就要找一个小母狗 作为李狗蛋的交配对象。
操作如下:
SELECT * FROM pet WHERE species = ‘dog‘ AND sex = ‘f‘;
mysql> SELECT *
-> FROM pet
-> WHERE species = ‘dog‘ AND sex = ‘f‘;
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
1 row in set (0.00 sec)
mysql>
很好,我们李家有后了。
还是一个栗子:没有故事了
操作如下:
SELECT * FROM get WHERE species = ‘snake‘ OR species = ‘bird‘;
mysql> SELECT *
-> FROM pet
-> WHERE species = ‘snake‘ OR species = ‘bird‘;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+-------+---------+------+------------+-------+
3 rows in set (0.00 sec)
mysql>
又是一个栗子:真的没有故事了 只是想说AND 和OR 可以混用,but AND 比 OR 具有更高的优先级,So像我这样的老司机都是加括号的(为了方便愚蠢的凡人们阅读)。
操作如下:
SELECT * FROM pet WHERE (species = ‘cat‘ AND sex = ‘m‘) OR (species = ‘dog‘ AND sex = ‘f‘);
mysql> SELECT *
-> FROM pet
-> WHERE (species = ‘cat‘ AND sex = ‘m‘)
-> OR (species = ‘dog‘ AND sex = ‘f‘);
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)
mysql>
好气呀,后面还有很多没有讲完。