记录操作:INSERT/UPDATE/DELETE/SELECT
1.插入记录(共有三种)
第一种: 表达式
1.INSERT [INTO] tbl_name [(col_name,...)] {VALUES|VALUE}({expr|DEFAULT},...),(...),... mysql> CREATE TABLE users( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(20) NOT NULL, -> password VARCHAR(32) NOT NULL, -> age TINYINT UNSIGNED NOT NULL DEFAULT 10, -> sex BOOLEAN -> ); Query OK, 0 rows affected (0.16 sec) mysql> INSERT INTO users VALUES(NULL,‘Tom‘,‘123‘,25,1); Query OK, 1 row affected (0.06 sec) mysql> SELECT * FROM users; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 25 | 1 | +----+----------+----------+-----+------+ 1 row in set (0.00 sec) mysql> INSERT INTO users VALUES(NULL,‘JULY‘,‘123‘,25,1); Query OK, 1 row affected (0.06 sec) mysql> SELECT * FROM users; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 25 | 1 | | 2 | JULY | 123 | 25 | 1 | +----+----------+----------+-----+------+ 2 rows in set (0.00 sec) mysql> INSERT INTO users VALUES(DEFAULT,‘Tom‘,‘123‘,25,1); Query OK, 1 row affected (0.05 sec) mysql> SELECT * FROM users; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 25 | 1 | | 2 | JULY | 123 | 25 | 1 | | 3 | Tom | 123 | 25 | 1 | +----+----------+----------+-----+------+ 3 rows in set (0.00 sec) mysql> INSERT INTO users VALUES(NULL,‘JULY‘,‘123‘,2*9*6-9,1); Query OK, 1 row affected (0.08 sec) mysql> SELECT * FROM users; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 25 | 1 | | 2 | JULY | 123 | 25 | 1 | | 3 | Tom | 123 | 25 | 1 | | 4 | JULY | 123 | 99 | 1 | +----+----------+----------+-----+------+ 4 rows in set (0.00 sec) mysql> INSERT INTO users VALUES(DEFAULT,‘Tom‘,‘123‘,DEFAULT,1); Query OK, 1 row affected (0.06 sec) mysql> SELECT * FROM users; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 25 | 1 | | 2 | JULY | 123 | 25 | 1 | | 3 | Tom | 123 | 25 | 1 | | 4 | JULY | 123 | 99 | 1 | | 5 | Tom | 123 | 10 | 1 | +----+----------+----------+-----+------+ 5 rows in set (0.00 sec) mysql> INSERT INTO users VALUES(NULL,‘JULY‘,‘123‘,2*9*6-9,1),(DEFAULT,‘King‘,‘123‘,DEFAULT,0); Query OK, 2 rows affected (0.05 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM users; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 25 | 1 | | 2 | JULY | 123 | 25 | 1 | | 3 | Tom | 123 | 25 | 1 | | 4 | JULY | 123 | 99 | 1 | | 5 | Tom | 123 | 10 | 1 | | 6 | JULY | 123 | 99 | 1 | | 7 | King | 123 | 10 | 0 | +----+----------+----------+-----+------+ 7 rows in set (0.00 sec) md5是php的一个函数,用来计算字符串的hash值 mysql> INSERT INTO users VALUES(DEFAULT,‘Tom‘,md5(‘123‘),DEFAULT,1); Query OK, 1 row affected (0.06 sec) mysql> SELECT * FROM users; +----+----------+----------------------------------+-----+------+ | id | username | password | age | sex | +----+----------+----------------------------------+-----+------+ | 1 | Tom | 123 | 25 | 1 | | 2 | JULY | 123 | 25 | 1 | | 3 | Tom | 123 | 25 | 1 | | 4 | JULY | 123 | 99 | 1 | | 5 | Tom | 123 | 10 | 1 | | 6 | JULY | 123 | 99 | 1 | | 7 | King | 123 | 10 | 0 | | 8 | Tom | 202cb962ac59075b964b07152d234b70 | 10 | 1 | +----+----------+----------------------------------+-----+------+ 8 rows in set (0.00 sec)
第二种:(如果看不懂这,先往下看,看完再回来看就懂了)
说明:与第一种方式的区别在于,此方法可以使用子查询(SubQuery)。并且一次只能插入一条数据。 INSERT [INTO] tbl_name SET col_name={expr|DEFAULT},... mysql> CREATE TABLE test( -> id TINYINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(20) -> ); Query OK, 0 rows affected (0.18 sec) mysql> SELECT * FROM test; Empty set (0.00 sec) mysql> INSERT test SELECT username FROM users WHERE age>=30; ERROR 1136 (21S01): Column count doesn‘t match value count at row 1 mysql> INSERT test(username) SELECT username FROM users WHERE age>=30; Query OK, 2 rows affected (0.06 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM test; +----+----------+ | id | username | +----+----------+ | 1 | JULY | | 2 | JULY | +----+----------+ 2 rows in set (0.00 sec)
第三种:
说明:此方法可以将查询结果插入到指定数据表。
INSERT [INTO] tbl_name [(col_name,...)] SELECT ...
2.更新记录(有两种方式)
第一种:单表更新
UPDATE [LOW_PRIORITY][IGNORE] table_reference SET col_name={expr1|DEFAULT}[,col_name2={expr2|DEFAULT}]... [WHERE where_condition] mysql> UPDATE users SET age=age+5; Query OK, 9 rows affected (0.09 sec) Rows matched: 9 Changed: 9 Warnings: 0 mysql> UPDATE users SET age=age+10,sex=0 WHERE id%2=0; Query OK, 4 rows affected (0.06 sec) Rows matched: 4 Changed: 4 Warnings: 0
第二种:多表更新
3.删除记录(有两种方式)
第一种:单表删除
DELETE FROM tbl_name [WHERE where_condition] DELETE FROM users WHERE id =6;
第二种:多表删除
4.查找记录
SELECT select_expr[,select_expr...] [ FROM table_reference [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 3+5; +-----+ | 3+5 | +-----+ | 8 | +-----+
查询表达式
每一个表达式表示想要的一列,必须有至少一个。
多个列之间以英文逗号分隔。
星号(*)表示所有列。tbl_name.* 可以表示命名表的所有列。
查询表达式可以使用[AS] alias_name 为其赋予别名。
别名可用于GROUP BY ,ORDER BY 或 HAVING 子句。
查询所有
mysql> SELECT * FROM users; +----+----------+----------------------------------+-----+------+ | id | username | password | age | sex | +----+----------+----------------------------------+-----+------+ | 1 | Tom | 123 | 29 | 0 | | 2 | JULY | 123 | 38 | 0 | | 3 | Tom | 123 | 27 | 0 | | 4 | JULY | 123 | 110 | 0 | | 5 | Tom | 123 | 10 | 0 | | 7 | King | 123 | 8 | 0 | | 8 | Tom | 202cb962ac59075b964b07152d234b70 | 17 | 0 | | 9 | Lucy | 123 | 6 | 0 | | 10 | qqqqq | 123 | 10 | NULL | +----+----------+----------------------------------+-----+------+
按查询次序排序
mysql> SELECT username,id from users; +----------+----+ | username | id | +----------+----+ | Tom | 1 | | JULY | 2 | | Tom | 3 | | JULY | 4 | | Tom | 5 | | King | 7 | | Tom | 8 | | Lucy | 9 | | qqqqq | 10 | +----------+----+
查询指定表的字段
mysql> SELECT users.id,users.username FROM users; +----+----------+ | id | username | +----+----------+ | 1 | Tom | | 2 | JULY | | 3 | Tom | | 4 | JULY | | 5 | Tom | | 7 | King | | 8 | Tom | | 9 | Lucy | | 10 | qqqqq | +----+----------+
‘AS‘的使用
mysql> SELECT id username FROM users;不标准 +----------+ | username | +----------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 7 | | 8 | | 9 | | 10 | +----------+ 9 rows in set (0.00 sec) mysql> SELECT id AS username FROM users;标准 +----------+ | username | +----------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 7 | | 8 | | 9 | | 10 | +----------+
4.1.条件表达式
对记录进行过滤,如果没有指定WHERE子句,则显示所有记录。
在WHERE表达式中,可以使用MYSQL支持的函数或运算符。
4.2.查询结果分组
[GROUP BY {col_name|position} [ASC|DESC],...]
根据sex字段分组
mysql> SELECT sex FROM users GROUP BY sex; +------+ | sex | +------+ | NULL | | 0 | +------+ 2 rows in set (0.00 sec)
根据查询的第一个字段分组
mysql> SELECT sex FROM users GROUP BY 1; +------+ | sex | +------+ | NULL | | 0 | +------+ 2 rows in set (0.00 sec)
根据查询的第二个字段分组
mysql> SELECT sex,username FROM users GROUP BY 2; +------+----------+ | sex | username | +------+----------+ | 0 | JULY | | 0 | King | | 0 | Lucy | | NULL | qqqqq | | 0 | Tom | +------+----------+ 5 rows in set (0.00 sec)
4.3分组条件
[HAVING where_condition] hava后用到的字段要么是聚合函数,要么是出现在前面的字段否则报错。 mysql> SELECT sex FROM users GROUP BY 1 HAVING age >5; ERROR 1054 (42S22): Unknown column ‘age‘ in ‘having clause‘ mysql> SELECT sex,age FROM users GROUP BY 1,2 HAVING age >5; +------+-----+ | sex | age | +------+-----+ | NULL | 10 | | 0 | 6 | | 0 | 8 | | 0 | 10 | | 0 | 17 | | 0 | 27 | | 0 | 29 | | 0 | 38 | | 0 | 110 | +------+-----+ 9 rows in set (0.00 sec) mysql> SELECT sex,age FROM users GROUP BY 1 HAVING age >5; +------+-----+ | sex | age | +------+-----+ | NULL | 10 | | 0 | 29 | +------+-----+ 2 rows in set (0.00 sec) mysql> SELECT sex,age FROM users GROUP BY 1 HAVING count(id) >2; +------+-----+ | sex | age | +------+-----+ | 0 | 29 |#四舍五入后的平均值 +------+-----+
4.4对查询结果进行排序
[ORDER BY {col_name|expr|position}[ASC|DESC],...]
默认按插入顺序
mysql> SELECT * FROM users; +----+----------+----------------------------------+-----+------+ | id | username | password | age | sex | +----+----------+----------------------------------+-----+------+ | 1 | Tom | 123 | 29 | 0 | | 2 | JULY | 123 | 38 | 0 | | 3 | Tom | 123 | 27 | 0 | | 4 | JULY | 123 | 110 | 0 | | 5 | Tom | 123 | 10 | 0 | | 7 | King | 123 | 8 | 0 | | 8 | Tom | 202cb962ac59075b964b07152d234b70 | 17 | 0 | | 9 | Lucy | 123 | 6 | 0 | | 10 | qqqqq | 123 | 10 | NULL | +----+----------+----------------------------------+-----+------+ 9 rows in set (0.00 sec)
ORDER BY 默认是升序
mysql> SELECT * FROM users ORDER BY age; +----+----------+----------------------------------+-----+------+ | id | username | password | age | sex | +----+----------+----------------------------------+-----+------+ | 9 | Lucy | 123 | 6 | 0 | | 7 | King | 123 | 8 | 0 | | 5 | Tom | 123 | 10 | 0 | | 10 | qqqqq | 123 | 10 | NULL | | 8 | Tom | 202cb962ac59075b964b07152d234b70 | 17 | 0 | | 3 | Tom | 123 | 27 | 0 | | 1 | Tom | 123 | 29 | 0 | | 2 | JULY | 123 | 38 | 0 | | 4 | JULY | 123 | 110 | 0 | +----+----------+----------------------------------+-----+------+ 9 rows in set (0.00 sec)
降序
mysql> SELECT * FROM users ORDER BY age DESC; +----+----------+----------------------------------+-----+------+ | id | username | password | age | sex | +----+----------+----------------------------------+-----+------+ | 4 | JULY | 123 | 110 | 0 | | 2 | JULY | 123 | 38 | 0 | | 1 | Tom | 123 | 29 | 0 | | 3 | Tom | 123 | 27 | 0 | | 8 | Tom | 202cb962ac59075b964b07152d234b70 | 17 | 0 | | 5 | Tom | 123 | 10 | 0 | | 10 | qqqqq | 123 | 10 | NULL | | 7 | King | 123 | 8 | 0 | | 9 | Lucy | 123 | 6 | 0 | +----+----------+----------------------------------+-----+------+ 9 rows in set (0.00 sec)
4.5限制查询结果返回的数量
[LIMIT {[offset,] row_count | row_count OFFSET offset}] mysql> SELECT * FROM users; +----+----------+----------------------------------+-----+------+ | id | username | password | age | sex | +----+----------+----------------------------------+-----+------+ | 1 | Tom | 123 | 29 | 0 | | 2 | JULY | 123 | 38 | 0 | | 3 | Tom | 123 | 27 | 0 | | 4 | JULY | 123 | 110 | 0 | | 5 | Tom | 123 | 10 | 0 | | 7 | King | 123 | 8 | 0 | | 8 | Tom | 202cb962ac59075b964b07152d234b70 | 17 | 0 | | 9 | Lucy | 123 | 6 | 0 | | 10 | qqqqq | 123 | 10 | NULL | +----+----------+----------------------------------+-----+------+ 9 rows in set (0.00 sec)
查询两条
mysql> SELECT * FROM users LIMIT 2; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 29 | 0 | | 2 | JULY | 123 | 38 | 0 | +----+----------+----------+-----+------+ 2 rows in set (0.00 sec)
查询从下标3开始,查两条。(MYSQL下标从0开始)
mysql> SELECT * FROM users LIMIT 3,2; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 4 | JULY | 123 | 110 | 0 | | 5 | Tom | 123 | 10 | 0 | +----+----------+----------+-----+------+ 2 rows in set (0.00 sec)
查询从下标2开始,查两条。(MYSQL下标从0开始)
mysql> SELECT * FROM users LIMIT 2,2; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 3 | Tom | 123 | 27 | 0 | | 4 | JULY | 123 | 110 | 0 | +----+----------+----------+-----+------+ 2 rows in set (0.00 sec)
查询降序后从下标2开始,查两条。
mysql> SELECT * FROM users ORDER BY age DESC; +----+----------+----------------------------------+-----+------+ | id | username | password | age | sex | +----+----------+----------------------------------+-----+------+ | 4 | JULY | 123 | 110 | 0 | | 2 | JULY | 123 | 38 | 0 | | 1 | Tom | 123 | 29 | 0 | | 3 | Tom | 123 | 27 | 0 | | 8 | Tom | 202cb962ac59075b964b07152d234b70 | 17 | 0 | | 5 | Tom | 123 | 10 | 0 | | 10 | qqqqq | 123 | 10 | NULL | | 7 | King | 123 | 8 | 0 | | 9 | Lucy | 123 | 6 | 0 | +----+----------+----------------------------------+-----+------+ mysql> SELECT * FROM users ORDER BY age DESC LIMIT 2,2; +----+----------+----------+-----+------+ | id | username | password | age | sex | +----+----------+----------+-----+------+ | 1 | Tom | 123 | 29 | 0 | | 3 | Tom | 123 | 27 | 0 | +----+----------+----------+-----+------+