文章目录
- 1. 添加记录
- 1.1 不指定字段名称
- 1.2 一次插入多条记录
- 1.3 INSERT ... SET 的形式
- 2. 修改记录
- 3. 删除记录
- 4. 彻底清空数据表
- 5. 查询记录
- 5.1 WHERE条件进行查询
- 5.2 检测null值
- 5.3 [NOT]BETWEEN ...AND
- 5.4 [NOT] IN()
- 5.5 逻辑运算符 AND , OR, NOT
- 5.6 [NOT] LIKE 模糊查询, 通配符 %, _
- 5.7 GROUP BY
- 5.8 group by 配合聚合函数来使用
- 5.9 HAVING
- 5.10 ORDER BY 排序
- 5.11 随机打乱的函数
- LIMIT 限制结果集显示条数,实现分页效果
1. 添加记录
先创建数据库和数据表
mysql> CREATE DATABASE IF NOT EXISTS king DEFAULT CHARACTER SET 'UTF8';
Query OK, 1 row affected, 1 warning (0.11 sec)
mysql> USE king;
Database changed
mysql> CREATE TABLE IF NOT EXISTS user(
-> id INT UNSIGNED AUTO_INCREMENT KEY COMMENT '编号',
-> username VARCHAR(20) NOT NULL UNIQUE COMMENT '用户名',
-> age TINYINT UNSIGNED DEFAULT 18 COMMENT '年龄',
-> email VARCHAR(50) NOT NULL DEFAULT '1111@qq.com' COMMENT '邮箱'
-> )ENGINE=INNODB CHARSET=UTF8;
Query OK, 0 rows affected, 1 warning (1.43 sec)
mysql> SELECT DATEBASE;
ERROR 1054 (42S22): Unknown column 'DATEBASE' in 'field list'
mysql> SELECT DATEBASE();
ERROR 1305 (42000): FUNCTION king.DATEBASE does not exist
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| king |
+------------+
1 row in set (0.00 sec)
mysql> SHOW TABLES;
+----------------+
| Tables_in_king |
+----------------+
| user |
+----------------+
1 row in set (0.02 sec)
mysql> DESC user;
+----------+------------------+------+-----+-------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+-------------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | UNI | NULL | |
| age | tinyint unsigned | YES | | 18 | |
| email | varchar(50) | NO | | 1111@qq.com | |
+----------+------------------+------+-----+-------------+----------------+
4 rows in set (0.00 sec)
1.1 不指定字段名称
mysql> INSERT user VALUE(1, 'KING', 24, '1212@qq.com');
Query OK, 1 row affected (0.07 sec)
mysql> INSERT user VALUES(NULL, 'queue', 25, 'queue@qq.com');
Query OK, 1 row affected (0.09 sec)
mysql> INSERT user VALUES(DEFAULT,'LILI', 26, 'lili@qq.com');
Query OK, 1 row affected (0.06 sec)
mysql> SELECT * FROM user;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | KING | 24 | 1212@qq.com |
| 2 | queue | 25 | queue@qq.com |
| 3 | LILI | 26 | lili@qq.com |
+----+----------+------+--------------+
3 rows in set (0.00 sec)
列出指定字段,每一个字段与后边的值一一对应。
mysql> INSERT user(username, email) VALUES('rose', 'rose@qq.com');
Query OK, 1 row affected (0.09 sec)
mysql> INSERT user(username, age, email) VALUES('rosa', 20, 'rosa@qq.com');
Query OK, 1 row affected (0.05 sec)
mysql> SELECT * FROM user;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | KING | 24 | 1212@qq.com |
| 2 | queue | 25 | queue@qq.com |
| 3 | LILI | 26 | lili@qq.com |
| 4 | rose | 18 | rose@qq.com |
| 6 | rosa | 20 | rosa@qq.com |
+----+----------+------+--------------+
5 rows in set (0.00 sec)
1.2 一次插入多条记录
mysql> INSERT user VALUES(NULL, 'a', DEFAULT, DEFAULT),
-> (NULL, 'b', 56, 'b@qq.com'),
-> (NULL, 'c', 34, 'c@qq.com');
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM user;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | KING | 24 | 1212@qq.com |
| 2 | queue | 25 | queue@qq.com |
| 3 | LILI | 26 | lili@qq.com |
| 4 | rose | 18 | rose@qq.com |
| 6 | rosa | 20 | rosa@qq.com |
| 7 | a | 18 | 1111@qq.com |
| 8 | b | 56 | b@qq.com |
| 9 | c | 34 | c@qq.com |
+----+----------+------+--------------+
8 rows in set (0.00 sec)
1.3 INSERT … SET 的形式
INSERT tbl_name SET 字段名称=值, …;
一次只能插入一条记录
mysql> INSERT user SET username='d', age=45, email='d@qq.com';
Query OK, 1 row affected (0.06 sec)
mysql> SELECT * FROM user;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | KING | 24 | 1212@qq.com |
| 2 | queue | 25 | queue@qq.com |
| 3 | LILI | 26 | lili@qq.com |
| 4 | rose | 18 | rose@qq.com |
| 6 | rosa | 20 | rosa@qq.com |
| 7 | a | 18 | 1111@qq.com |
| 8 | b | 56 | b@qq.com |
| 9 | c | 34 | c@qq.com |
| 10 | d | 45 | d@qq.com |
+----+----------+------+--------------+
9 rows in set (0.01 sec)
查数据库中的插入操作,如果为自动编号的字段赋值的话,我们可以用NULL或DEFAULT让其按照默认的递增的形式来实现。给有默认值的字段赋值时,可以用DEFAULT让其采用默认值。
2. 修改记录
语法:
UPDATE tbl_name SET 字段名称=值,字段名称=值[EHRER 条件]
如果不添加条件,整个表中的记录都会被更新
mysql> UPDATE user SET age=29 WHERE id=1;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM user
-> ;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | KING | 29 | 1212@qq.com |
| 2 | queue | 25 | queue@qq.com |
| 3 | LILI | 26 | lili@qq.com |
| 4 | rose | 18 | rose@qq.com |
| 6 | rosa | 20 | rosa@qq.com |
| 7 | a | 18 | 1111@qq.com |
| 8 | b | 56 | b@qq.com |
| 9 | c | 34 | c@qq.com |
| 10 | d | 45 | d@qq.com |
+----+----------+------+--------------+
9 rows in set (0.00 sec)
3. 删除记录
delete FROM tbl_name WHERE
如果不添加条件,表中所有字段都会被删除
mysql> DELETE FROM user WHERE username='KING';
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM user;
+----+----------+------+-------------+
| id | username | age | email |
+----+----------+------+-------------+
| 2 | queue | 30 | 1111@qq.com |
| 3 | li | 28 | 1111@qq.com |
| 4 | rose | 23 | 1111@qq.com |
| 6 | rosa | 30 | rosa@qq.com |
| 7 | a | 28 | 1111@qq.com |
| 8 | b | 66 | b@qq.com |
| 9 | c | 44 | c@qq.com |
| 10 | d | 55 | d@qq.com |
+----+----------+------+-------------+
8 rows in set (0.03 sec)
mysql> SELECT * FROM user;
+----+----------+------+-------------+
| id | username | age | email |
+----+----------+------+-------------+
| 2 | queue | 30 | 1111@qq.com |
| 3 | li | 28 | 1111@qq.com |
| 4 | rose | 23 | 1111@qq.com |
| 6 | rosa | 30 | rosa@qq.com |
| 7 | a | 28 | 1111@qq.com |
| 8 | b | 66 | b@qq.com |
| 9 | c | 44 | c@qq.com |
| 10 | d | 55 | d@qq.com |
+----+----------+------+-------------+
8 rows in set (0.03 sec)
mysql> DELETE FROM user;
Query OK, 6 rows affected (0.07 sec)
mysql> SELECT * FROM user;
Empty set (0.02 sec)
但是这样删除表数据后,对于自增长的字段,再你下次添加的时候,并不是从1开始的,
mysql> INSERT user VALUES(NULL, 'a', DEFAULT, DEFAULT),
-> (NULL, 'b', 56, 'b@qq.com'),
-> (NULL, 'c', 34, 'c@qq.com');
Query OK, 3 rows affected (0.10 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM user;
+----+----------+------+-------------+
| id | username | age | email |
+----+----------+------+-------------+
| 11 | a | 18 | 1111@qq.com |
| 12 | b | 56 | b@qq.com |
| 13 | c | 34 | c@qq.com |
+----+----------+------+-------------+
3 rows in set (0.01 sec)
mysql> SHOW CREATE TABLE user;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user | CREATE TABLE `user` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '编号',
`username` varchar(20) NOT NULL COMMENT '用户名',
`age` tinyint unsigned DEFAULT '18' COMMENT '年龄',
`email` varchar(50) NOT NULL DEFAULT '1111@qq.com' COMMENT '邮箱',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
想要下次插入数据时候,id值从1开始,就需要下面语句
ALTER TABLE user AUTO_INCREMENT=1;
(!!!注意,只有在删除表中所有数据后(DELETE FROM user),设置才有效)
mysql> DELETE FROM user;
Query OK, 4 rows affected (0.13 sec)
mysql> INSERT user VALUES(NULL, 'queue', 25, 'queue@qq.com');
Query OK, 1 row affected (0.07 sec)
mysql> SELECT * FROM user;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | queue | 25 | queue@qq.com |
+----+----------+------+--------------+
1 row in set (0.00 sec)
4. 彻底清空数据表
TRUNCATE [TABLE] tbl_name;
会清除表中所有记录,并重置AUTO_CREMENT的值
mysql> TRUNCATE user;
Query OK, 0 rows affected (0.85 sec)
mysql> SELECT * FROM user;
Empty set (0.08 sec)
mysql> INSERT user SET username='d', age=45, email='d@qq.com';
Query OK, 1 row affected (0.09 sec)
mysql> SELECT * FROM user;
+----+----------+------+----------+
| id | username | age | email |
+----+----------+------+----------+
| 1 | d | 45 | d@qq.com |
+----+----------+------+----------+
1 row in set (0.00 sec)
5. 查询记录
查询字段
SELECT select_expr, … FROM tbl_name
[WHERE 条件]
[GROUP BY {col_name | position} HAVING 二次筛选]
[ORDER BY {col_name | position | expr}{ASC | DESC}]
[LIMIT 限制结果集的显示条数]
*代表查询整个表中所有信息
mysql> SELECT * FROM user;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | d | 45 | d@qq.com |
| 2 | queue | 25 | queue@qq.com |
| 3 | LILI | 26 | lili@qq.com |
| 6 | KING | 24 | 1212@qq.com |
+----+----------+------+--------------+
4 rows in set (0.00 sec)
mysql> SELECT username, age FROM user;
+----------+------+
| username | age |
+----------+------+
| d | 45 |
| queue | 25 |
| LILI | 26 |
| KING | 24 |
+----------+------+
4 rows in set (0.00 sec)
可以使用库名.表名的形式查询数据,这样的好处是可以不用打开数据库
直接进行查询
mysql> SELECT * FROM king.user;
+----+----------+------+--------------+
| id | username | age | email |
+----+----------+------+--------------+
| 1 | d | 45 | d@qq.com |
| 2 | queue | 25 | queue@qq.com |
| 3 | LILI | 26 | lili@qq.com |
| 6 | KING | 24 | 1212@qq.com |
+----+----------+------+--------------+
4 rows in set (0.00 sec)
起别名
mysql> SELECT username AS user FROM user AS u;
+-------+
| user |
+-------+
| d |
| KING |
| LILI |
| queue |
+-------+
4 rows in set (0.00 sec)
查询表达式中的顺序可以和原数据表中字段的顺序不一致且SELECT中查询表达式将影响着结果的顺序
5.1 WHERE条件进行查询
mysql> SELECT username FROM user WHERE id=3;
+----------+
| username |
+----------+
| LILI |
+----------+
1 row in set (0.01 sec)
如果没有找到要查询的结果,返回空
mysql> SELECT * FROM user;
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 1 | d | 45 | d@qq.com | this is a test |
| 2 | queue | 25 | queue@qq.com | this is a test |
| 3 | LILI | 26 | lili@qq.com | this is a test |
| 6 | KING | 24 | 1212@qq.com | NULL |
+----+----------+------+--------------+----------------+
4 rows in set (0.00 sec)
mysql> SELECT id, username, age WHERE userDes=NULL;
ERROR 1054 (42S22): Unknown column 'id' in 'field list'
mysql> SELECT id, username, age FROM user WHERE userDes=NULL;
Empty set (0.00 sec)
mysql> SELECT id, username, age FROM user WHERE userDes<=>NULL;
+----+----------+------+
| id | username | age |
+----+----------+------+
| 6 | KING | 24 |
+----+----------+------+
1 row in set (0.00 sec)
5.2 检测null值
–检测null值方案1
SELECT id, username, age FROM user WHERE userDes<=>NULL;
–检测null值方案2
SELECT id, username, age FROM user WHERE userDes IS [NOT] NULL;
mysql> SELECT id, username, age FROM user WHERE userDes<=>NULL;
+----+----------+------+
| id | username | age |
+----+----------+------+
| 6 | KING | 24 |
+----+----------+------+
1 row in set (0.00 sec)
mysql> SELECT id, username, age FROM user WHERE userDes IS NULL;
+----+----------+------+
| id | username | age |
+----+----------+------+
| 6 | KING | 24 |
+----+----------+------+
1 row in set (0.00 sec)
5.3 [NOT]BETWEEN …AND
左右都包含
mysql> SELECT id,username,age FROM user WHERE age BETWEEN 23 AND 26;
+----+----------+------+
| id | username | age |
+----+----------+------+
| 2 | queue | 25 |
| 3 | LILI | 26 |
| 6 | KING | 24 |
+----+----------+------+
3 rows in set (0.00 sec)
5.4 [NOT] IN()
mysql> SELECT id, username,age FROM user WHERE age IN (23, 24, 25);
+----+----------+------+
| id | username | age |
+----+----------+------+
| 2 | queue | 25 |
| 6 | KING | 24 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql> SELECT id, username,age FROM user WHERE age NOT IN (23, 24, 25);
+----+----------+------+
| id | username | age |
+----+----------+------+
| 1 | d | 45 |
| 3 | LILI | 26 |
+----+----------+------+
2 rows in set (0.00 sec)
5.5 逻辑运算符 AND , OR, NOT
mysql> SELECT id, username,age FROM user WHERE age=25 AND id=2;
+----+----------+------+
| id | username | age |
+----+----------+------+
| 2 | queue | 25 |
+----+----------+------+
1 row in set (0.00 sec)
5.6 [NOT] LIKE 模糊查询, 通配符 %, _
% 代表可以匹配任意字符的
_可以匹配一个字符
默认忽略大小写
mysql> SELECT id,username,age FROM user WHERE username LIKE '%i%';
+----+----------+------+
| id | username | age |
+----+----------+------+
| 3 | LILI | 26 |
| 6 | KING | 24 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql> SELECT id,username,age FROM user WHERE username LIKE '%_e%';
+----+----------+------+
| id | username | age |
+----+----------+------+
| 2 | queue | 25 |
+----+----------+------+
1 row in set (0.00 sec)
5.7 GROUP BY
把值相同的放到一个组中,最终查询结果只会显示组中的一条记录
mysql> SELECT id,username FROM user GROUP BY userDes;
+----+----------+
| id | username |
+----+----------+
| 1 | d |
| 6 | KING |
+----+----------+
2 rows in set (0.01 sec)
分组配合GROUP_CONCAT()查看组中某个字段的详细信息
mysql> SELECT GROUP_CONCAT(username),id FROM user GROUP BY userDes;
+------------------------+----+
| GROUP_CONCAT(username) | id |
+------------------------+----+
| KING | 6 |
| d,queue,LILI | 1 |
+------------------------+----+
2 rows in set (0.04 sec)
5.8 group by 配合聚合函数来使用
COUNT(),
MAX()
MIN()
AVG();
mysql> SELECT count(userDes) FROM user GROUP BY userDes;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 11
Current database: king
+----------------+
| count(userDes) |
+----------------+
| 3 |
| 0 |
+----------------+
2 rows in set (0.04 sec)
如果通过分组,直接查询字段, 仅会显示每个组中的一条信息,分组配合GROUP_CONCAT()查看组中某个字段的详细信息
mysql> SELECT id,username FROM user GROUP BY userDes;
+----+----------+
| id | username |
+----+----------+
| 1 | d |
| 6 | KING |
+----+----------+
2 rows in set (0.01 sec)
mysql> SELECT GROUP_CONCAT(username),id FROM user GROUP BY userDes;
+------------------------+----+
| GROUP_CONCAT(username) | id |
+------------------------+----+
| KING | 6 |
| d,queue,LILI | 1 |
+------------------------+----+
2 rows in set (0.04 sec)
5.9 HAVING
用HAVING进行分组条件的指定时,一定要保证分组条件要么为聚合函
么条件中的字段必须出现在当前的SELECT语句中
5.10 ORDER BY 排序
语法:ORDER BY 字段名称 ASC | DESC
mysql> SELECT * FROM user;
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 1 | d | 45 | d@qq.com | this is a test |
| 2 | queue | 25 | queue@qq.com | this is a test |
| 3 | LILI | 26 | lili@qq.com | this is a test |
| 6 | KING | 24 | 1212@qq.com | NULL |
+----+----------+------+--------------+----------------+
4 rows in set (0.00 sec)
按照年龄降序
mysql> SELECT * FROM user
-> ORDER BY age DESC;
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 1 | d | 45 | d@qq.com | this is a test |
| 3 | LILI | 26 | lili@qq.com | this is a test |
| 2 | queue | 25 | queue@qq.com | this is a test |
| 6 | KING | 24 | 1212@qq.com | NULL |
+----+----------+------+--------------+----------------+
4 rows in set (0.00 sec)
按照多个字段排序
语法:
ORDER BY 字段名称 ASC | DESC , 字段名称 ASC | DESC…
按照年龄升序,当年龄相同时,按照id降序
mysql> SELECT * FROM user
-> ORDER BY age DESC, id DESC;
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 1 | d | 45 | d@qq.com | this is a test |
| 3 | LILI | 26 | lili@qq.com | this is a test |
| 2 | queue | 25 | queue@qq.com | this is a test |
| 7 | ee | 24 | ee@qq.com | NULL |
| 6 | KING | 24 | 1212@qq.com | NULL |
+----+----------+------+--------------+----------------+
5 rows in set (0.00 sec)
5.11 随机打乱的函数
SELECT RAND();# 它每次会产生一个0-1之间的随机数
mysql> SELECT RAND();
+--------------------+
| RAND() |
+--------------------+
| 0.8787515301990803 |
+--------------------+
1 row in set (0.02 sec)
mysql> SELECT RAND();
+---------------------+
| RAND() |
+---------------------+
| 0.39015106334365074 |
+---------------------+
1 row in set (0.00 sec)
–实现随机记录
SELECT * FROM user ORDER BY RAND();
mysql> SELECT * FROM user ORDER BY RAND();
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 2 | queue | 25 | queue@qq.com | this is a test |
| 3 | LILI | 26 | lili@qq.com | this is a test |
| 1 | d | 45 | d@qq.com | this is a test |
| 6 | KING | 24 | 1212@qq.com | NULL |
| 7 | ee | 24 | ee@qq.com | NULL |
+----+----------+------+--------------+----------------+
5 rows in set (0.01 sec)
mysql> SELECT * FROM user ORDER BY RAND();
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 2 | queue | 25 | queue@qq.com | this is a test |
| 7 | ee | 24 | ee@qq.com | NULL |
| 3 | LILI | 26 | lili@qq.com | this is a test |
| 1 | d | 45 | d@qq.com | this is a test |
| 6 | KING | 24 | 1212@qq.com | NULL |
+----+----------+------+--------------+----------------+
5 rows in set (0.00 sec)
每次查询的结果都不同
LIMIT 限制结果集显示条数,实现分页效果
–语法1:LIMIT 数字
显示结果集的前几条数据
–语法2:LIMIT offset, row_cour
从offset开始,显示row_cour条记录,offset从0开始
和编号没有关系,和查询结果有关系
mysql> SELECT * FROM user LIMIT 3;
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 1 | d | 45 | d@qq.com | this is a test |
| 2 | queue | 25 | queue@qq.com | this is a test |
| 3 | LILI | 26 | lili@qq.com | this is a test |
+----+----------+------+--------------+----------------+
3 rows in set (0.03 sec)
分页查询
mysql> SELECT * FROM user LIMIT 0,3;
+----+----------+------+--------------+----------------+
| id | username | age | email | userDes |
+----+----------+------+--------------+----------------+
| 1 | d | 45 | d@qq.com | this is a test |
| 2 | queue | 25 | queue@qq.com | this is a test |
| 3 | LILI | 26 | lili@qq.com | this is a test |
+----+----------+------+--------------+----------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM user LIMIT 3,3;
+----+----------+------+-------------+---------+
| id | username | age | email | userDes |
+----+----------+------+-------------+---------+
| 6 | KING | 24 | 1212@qq.com | NULL |
| 7 | ee | 24 | ee@qq.com | NULL |
+----+----------+------+-------------+---------+
2 rows in set (0.00 sec)
注:UPDATE 或DELETE 时LIMIT只支持一个参数的形式