查找记录
SELECT select_expr[,select_expr...]
[
FROM tb_references
[WHERE where_condition]
[GROUP BY {col_name|position} [ASC|DESC],...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC|DESC],...]
[LIMIT {[offset,] row_count|row_count OFFSET offset}]
]
关于其中的一些名词解释
select_expr
*
)表示多有列。(tb_name.*
可以表示命名表的所有列)[AS] alias_name
为其赋予别名GROUP BY
,ORDER BY
或HAVING
子句WHERE
WHERE
子句,则显示所有记录WHERE
表达式中,可以使用MySQL
支持的函数或运算符。GROUP BY
HAVING
ORDER BY
LIMIT
SELECT
方法示例在下面的方法演示中都以这张数据表作演示
mysql> SELECT * FROM users2;
+----+----------+----------------------------------+-----+------+
| id | username | userpassword | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | AAA | IAMPASSWORD | 25 | 1 |
| 3 | BBB | IAMPASSWORD | 45 | 1 |
| 4 | CCC | *asfjdhia | 26 | 1 |
| 6 | DDD | *dsfjksp;flke | 32 | 1 |
| 8 | FFF | bc9c74cd849e1f1b99b16b0d92292bf6 | 50 | 0 |
| 9 | GGG | ahnkhFKAJ | 15 | NULL |
| 10 | WOO | **qwdfhjoQ | 25 | NULL |
+----+----------+----------------------------------+-----+------+
7 rows in set (0.01 sec)
例如查询id
字段、username
字段的内容
SELECT select_expr[,select_expr...]
mysql> SELECT id,username FROM users2;
+----+----------+
| id | username |
+----+----------+
| 1 | AAA |
| 3 | BBB |
| 4 | CCC |
| 6 | DDD |
| 8 | FFF |
| 9 | GGG |
| 10 | WOO |
+----+----------+
7 rows in set (0.00 sec)
mysql> SELECT username,id FROM users;
Empty set (0.01 sec)
mysql> SELECT username,id FROM users2;
+----------+----+
| username | id |
+----------+----+
| AAA | 1 |
| BBB | 3 |
| CCC | 4 |
| DDD | 6 |
| FFF | 8 |
| GGG | 9 |
| WOO | 10 |
+----------+----+
7 rows in set (0.00 sec)
可以发现两者表的顺序适合查询语句中的select_expr
顺序是一致的。
SELECT select_expr [AS] alias_name
将查询的id
字段以uid
,username
字段以uname
列出结果
mysql> SELECT id AS uid, username AS uname FROM users2;
+-----+-------+
| uid | uname |
+-----+-------+
| 1 | AAA |
| 3 | BBB |
| 4 | CCC |
| 6 | DDD |
| 8 | FFF |
| 9 | GGG |
| 10 | WOO |
+-----+-------+
7 rows in set (0.00 sec)
在表达式中[AS]
是允许不键入的,但是在实际操作中建议的还是都给写上,以免发生误解。例如以下操作:
它的意义为查询users2
表中的id字段的值,并取别名为username
展示出来。而不是查询users2
表中id
、username
字段的值。
mysql> SELECT id username FROM users2;
+----------+
| username |
+----------+
| 1 |
| 3 |
| 4 |
| 6 |
| 8 |
| 9 |
| 10 |
+----------+
7 rows in set (0.01 sec)
SELECT select_expr[,select_expr...] [GROUP BY {col_name|position} [ASC|DESC],...]
将查询结果以性别sex
进行分类,在表达式中ASC
表示以正序进行查找,DESC
表示以倒序进行查找。
mysql> SELECT * FROM users2 GROUP BY sex;
+----+----------+----------------------------------+-----+------+
| id | username | userpassword | age | sex |
+----+----------+----------------------------------+-----+------+
| 9 | GGG | ahnkhFKAJ | 15 | NULL |
| 8 | FFF | bc9c74cd849e1f1b99b16b0d92292bf6 | 50 | 0 |
| 1 | AAA | IAMPASSWORD | 25 | 1 |
+----+----------+----------------------------------+-----+------+
3 rows in set (0.01 sec)
[HAVING where_condition]
可以配合着上一条查询分组操作使用。
例如查询性别sex
字段并以sex
字段分组中的年龄age>20
的数据操作。
mysql> SELECT sex FROM users2 GROUP BY sex HAVING age > 20;
ERROR 1054 (42S22): Unknown column 'age' in 'having clause'
如果按照上面的查询语句会发现出错,出错的原因是以为查询的字段中没有包含age
字段,所以此时用age>20
来作为条件会出现错误。
所以需要将条件语句中用到的字段加入查询字段中,如下操作语句:
mysql> SELECT sex,age FROM users2 GROUP BY sex HAVING age > 20;
+------+-----+
| sex | age |
+------+-----+
| 0 | 50 |
| 1 | 25 |
+------+-----+
2 rows in set (0.01 sec)
mysql> SELECT * FROM users2 GROUP BY sex HAVING age > 20;
+----+----------+----------------------------------+-----+------+
| id | username | userpassword | age | sex |
+----+----------+----------------------------------+-----+------+
| 8 | FFF | bc9c74cd849e1f1b99b16b0d92292bf6 | 50 | 0 |
| 1 | AAA | IAMPASSWORD | 25 | 1 |
+----+----------+----------------------------------+-----+------+
2 rows in set (0.00 sec)
[LIMIT {offset,} row_count | row_count OFFSET offset}]
例如查询users2
表中的头两行数据:
mysql> SELECT * FROM users2 LIMIT 2
-> ;
+----+----------+--------------+-----+------+
| id | username | userpassword | age | sex |
+----+----------+--------------+-----+------+
| 1 | AAA | IAMPASSWORD | 25 | 1 |
| 3 | BBB | IAMPASSWORD | 45 | 1 |
+----+----------+--------------+-----+------+
2 rows in set (0.00 sec)
查询第4
行开始的2
行数据,需要注意的是LIMIT
中是以0
开始计算的,所以查询第4
行时应该加入LIMIT
的条件时应该是3=(4-1)
。
mysql> SELECT * FROM users2 LIMIT 3,2;
+----+----------+----------------------------------+-----+------+
| id | username | userpassword | age | sex |
+----+----------+----------------------------------+-----+------+
| 6 | DDD | *dsfjksp;flke | 32 | 1 |
| 8 | FFF | bc9c74cd849e1f1b99b16b0d92292bf6 | 50 | 0 |
+----+----------+----------------------------------+-----+------+
2 rows in set (0.00 sec)
[ORDER BY {col_name | expr | position} [ASC|DESC],...]
在上面说到了ASC
是升序,DESC
是降序。
例如查询users2
表中的所有字段,降序排列,并展示出排序结果后的第4
行开始的两行的数据。
mysql> SELECT * FROM users2 ORDER BY id DESC LIMIT 3,2;
+----+----------+---------------+-----+------+
| id | username | userpassword | age | sex |
+----+----------+---------------+-----+------+
| 6 | DDD | *dsfjksp;flke | 32 | 1 |
| 4 | CCC | *asfjdhia | 26 | 1 |
+----+----------+---------------+-----+------+
2 rows in set (0.00 sec)
新建一张数据表users3
,将在users2
表中查询到的数据放入users3
表中。
mysql> CREATE TABLE users3(
-> id TINYINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(20)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT * FROM users3;
Empty set (0.02 sec)
mysql> INSERT users3(username) SELECT username FROM users2 WHERE age>30;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM users3;
+----+----------+
| id | username |
+----+----------+
| 1 | BBB |
| 2 | DDD |
| 3 | FFF |
+----+----------+
3 rows in set (0.00 sec)