where条件
where条件可以使用的操作符
多个条件共同使用,使用and连接,如where id=1 and name=‘李华’
mysql> select id,name as 专业名称 from major; +----+-----------------------+ | id | 专业名称 | +----+-----------------------+ | 1 | 智能科学与技术 | | 2 | 智能制造 | | 3 | 智能制造 | | 4 | 智能制造 | | 5 | 智能制造 | +----+-----------------------+ 5 rows in set (0.00 sec) mysql> select id,name as 专业名称 from major where name='智能制造'; +----+--------------+ | id | 专业名称 | +----+--------------+ | 2 | 智能制造 | | 3 | 智能制造 | | 4 | 智能制造 | | 5 | 智能制造 | +----+--------------+ 4 rows in set (0.00 sec) mysql> select id,name as 专业名称 from major where name!='智能制造'; +----+-----------------------+ | id | 专业名称 | +----+-----------------------+ | 1 | 智能科学与技术 | +----+-----------------------+ 1 row in set (0.00 sec) mysql> select id,name as 专业名称 from major where not name='智能制造'; +----+-----------------------+ | id | 专业名称 | +----+-----------------------+ | 1 | 智能科学与技术 | +----+-----------------------+ 1 row in set (0.00 sec) mysql> select id,name as 专业名称 from major where name='智能制造' and id =2; +----+--------------+ | id | 专业名称 | +----+--------------+ | 2 | 智能制造 | +----+--------------+ 1 row in set (0.00 sec) mysql> select id,name as 专业名称 from major where id < 2; +----+-----------------------+ | id | 专业名称 | +----+-----------------------+ | 1 | 智能科学与技术 | +----+-----------------------+ 1 row in set (0.00 sec) mysql> select id,name as 专业名称 from major where id >= 2; +----+--------------+ | id | 专业名称 | +----+--------------+ | 2 | 智能制造 | | 3 | 智能制造 | | 4 | 智能制造 | | 5 | 智能制造 | +----+--------------+ 4 rows in set (0.00 sec) mysql> select id,name as 专业名称 from major where id >= 2 and id <= 4; +----+--------------+ | id | 专业名称 | +----+--------------+ | 2 | 智能制造 | | 3 | 智能制造 | | 4 | 智能制造 | +----+--------------+ 3 rows in set (0.00 sec) mysql> select id,name as 专业名称 from major where id = 2 or id = 4; +----+--------------+ | id | 专业名称 | +----+--------------+ | 2 | 智能制造 | | 4 | 智能制造 | +----+--------------+ 2 rows in set (0.00 sec)
判空
当然,还可以用来判空或者非空,并不是直接=null
,而是is null
和is not null
UPDATE sys_user SET avatar = concat( "https://hahaha/", FLOOR(RAND()*29+1), ".png" ) WHERE avatar IS NULL OR avatar = "";
and和or的使用
SELECT * FROM chat WHERE ( from_who = "admin" AND to_who = "user1" ) OR ( to_who = "admin" AND from_who = "user1" ) ORDER BY create_time DESC;
数据删除 DELETE
删除所有数据
delete from 表名;
使用delete删除所有数据,如果重启数据库,会出现如下现象
- InnoDB 自增列会从1开始(存在内存中,断电即失)
- MyISAM 自增列继续从上一个自增量开始(存在文件中,不会丢失)
删除指定数据
delete from 表名 where 条件;
数据表清空 TRUNCATE
清空一个数据表的所有数据,自增id计数器重新恢复到1,表的结构和索引约束不会改变
truncate 表名
数据查询语言Data Query LANGUAGE(DQL) SELECT
查询一个表中的所有数据
select * from 表名;
查询指定字段的数据
select 字段1,字段2,字段3 from 表名;
注意:能查询部分字段的,千万不要查询*,这样会增加查询时间
给字段取别名
mysql> select name from major; +-----------------------+ | name | +-----------------------+ | 智能科学与技术 | | 智能制造 | | 智能制造 | | 智能制造 | | 智能制造 | +-----------------------+ 5 rows in set (0.00 sec) mysql> select name as 专业名称 from major; +-----------------------+ | 专业名称 | +-----------------------+ | 智能科学与技术 | | 智能制造 | | 智能制造 | | 智能制造 | | 智能制造 | +-----------------------+ 5 rows in set (0.00 sec)
给表取别名
mysql> select student.id as 学号,student.name as 姓名,major.name as 专业 from student left join major on student.major_id = major.id; +--------+--------+-----------------------+ | 学号 | 姓名 | 专业 | +--------+--------+-----------------------+ | 1 | 李华 | 智能制造 | | 2 | 匿名 | 智能科学与技术 | +--------+--------+-----------------------+ 2 rows in set (0.00 sec) mysql> select s.id as 学号,s.name as 姓名,m.name as 专业 from student as s left join major as m on s.major_id = m.id; +--------+--------+-----------------------+ | 学号 | 姓名 | 专业 | +--------+--------+-----------------------+ | 1 | 李华 | 智能制造 | | 2 | 匿名 | 智能科学与技术 | +--------+--------+-----------------------+ 2 rows in set (0.00 sec)
取别名可以更方便开发者编写sql语句
字符串拼接
mysql> select concat('专业名称:',name) from major; +--------------------------------------+ | concat('专业名称:',name) | +--------------------------------------+ | 专业名称:智能科学与技术 | | 专业名称:智能制造 | | 专业名称:智能制造 | | 专业名称:智能制造 | | 专业名称:智能制造 | +--------------------------------------+ 5 rows in set (0.00 sec)
查询结果数据去重
重复的数据只显示一条
mysql> select name as 专业名称 from major; +-----------------------+ | 专业名称 | +-----------------------+ | 智能科学与技术 | | 智能制造 | | 智能制造 | | 智能制造 | | 智能制造 | +-----------------------+ 5 rows in set (0.00 sec) mysql> select distinct name as 专业名称 from major; +-----------------------+ | 专业名称 | +-----------------------+ | 智能科学与技术 | | 智能制造 | +-----------------------+ 2 rows in set (0.00 sec)
查询时增加计算
mysql> select id,name as 专业名称 from major; +----+-----------------------+ | id | 专业名称 | +----+-----------------------+ | 1 | 智能科学与技术 | | 2 | 智能制造 | | 3 | 智能制造 | | 4 | 智能制造 | | 5 | 智能制造 | +----+-----------------------+ 5 rows in set (0.00 sec) -- 将所有数据的 id 增加 1 mysql> select id+1,name as 专业名称 from major; +------+-----------------------+ | id+1 | 专业名称 | +------+-----------------------+ | 2 | 智能科学与技术 | | 3 | 智能制造 | | 4 | 智能制造 | | 5 | 智能制造 | | 6 | 智能制造 | +------+-----------------------+ 5 rows in set (0.00 sec)
模糊查询
连接查询
七种连接方式
https://blog.csdn.net/laodanqiu/article/details/131233741
自连接
用途:一个表同时存储了父类数据和子类数据,如省市区表同时存储了中国的所有省、市、区的数据,希望查询出广东省下面的所有城市
mysql> desc province_city_region; +-------------+-------------+------+-----+-------------------+-----------------------------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+-------------------+-----------------------------------------------+ | id | bigint | NO | PRI | NULL | auto_increment | | create_time | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED | | update_time | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP | | is_deleted | tinyint | YES | | 0 | | | name | varchar(50) | YES | | NULL | | | type | tinyint | YES | | NULL | | | parent_id | bigint | YES | | NULL | | +-------------+-------------+------+-----+-------------------+-----------------------------------------------+ 7 rows in set (0.00 sec) mysql> select a.id,a.name as 市名称,b.name as 省名称 from province_city_region as a,province_city_region as b where b.name="广东省" and a.parent_id=b.id; +------+-----------+-----------+ | id | 市名称 | 省名称 | +------+-----------+-----------+ | 4792 | 茂名市 | 广东省 | | 4798 | 湛江市 | 广东省 | | 4808 | 东莞市 | 广东省 | | 4809 | 江门市 | 广东省 | | 4817 | 清远市 | 广东省 | | 4826 | 佛山市 | 广东省 | | 4832 | 阳江市 | 广东省 | | 4837 | 汕头市 | 广东省 | | 4845 | 河源市 | 广东省 | | 4852 | 珠海市 | 广东省 | | 4856 | 汕尾市 | 广东省 | | 4861 | 深圳市 | 广东省 | | 4868 | 梅州市 | 广东省 | | 4877 | 揭阳市 | 广东省 | | 4883 | 韶关市 | 广东省 | | 4894 | 惠州市 | 广东省 | | 4900 | 潮州市 | 广东省 | | 4904 | 广州市 | 广东省 | | 4916 | 肇庆市 | 广东省 | | 4925 | 中山市 | 广东省 | | 4926 | 云浮市 | 广东省 | +------+-----------+-----------+ 21 rows in set (0.00 sec)
分页
【查询语句】limit 数据索引起始值 数据量的大小
limit 0,5
:查询的是第0,1,2,3,4条数据
limit 2,5
:查询的是第2,3,4,5,6条数据
排序
【查询语句】order by 字段 ASC / DESC
- ASC(升序)
- DESC(降序)
mysql> select * from province_city_region order by id asc limit 0,10; +------+---------------------+---------------------+------------+-----------+------+-----------+ | id | create_time | update_time | is_deleted | name | type | parent_id | +------+---------------------+---------------------+------------+-----------+------+-----------+ | 2928 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 山东省 | 0 | 0 | | 2929 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 莱芜市 | 1 | 2928 | | 2930 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 莱城区 | 2 | 2929 | | 2931 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 钢城区 | 2 | 2929 | | 2932 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 济南市 | 1 | 2928 | | 2933 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 历城区 | 2 | 2932 | | 2934 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 历下区 | 2 | 2932 | | 2935 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 长清区 | 2 | 2932 | | 2936 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 平阴县 | 2 | 2932 | | 2937 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 章丘市 | 1 | 2932 | +------+---------------------+---------------------+------------+-----------+------+-----------+ 10 rows in set (0.00 sec) mysql> select * from province_city_region order by id desc limit 0,10; +------+---------------------+---------------------+------------+--------------------------+------+-----------+ | id | create_time | update_time | is_deleted | name | type | parent_id | +------+---------------------+---------------------+------------+--------------------------+------+-----------+ | 5851 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 天峨县 | 2 | 5840 | | 5850 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 凤山县 | 2 | 5840 | | 5849 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 南丹县 | 2 | 5840 | | 5848 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 环江毛南族自治县 | 2 | 5840 | | 5847 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 巴马瑶族自治县 | 2 | 5840 | | 5846 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 东兰县 | 2 | 5840 | | 5845 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 金城江区 | 2 | 5840 | | 5844 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 罗城仫佬族自治县 | 2 | 5840 | | 5843 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 宜州市 | 1 | 5840 | | 5842 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 都安瑶族自治县 | 2 | 5840 | +------+---------------------+---------------------+------------+--------------------------+------+-----------+ 10 rows in set (0.00 sec)
子查询
注意
:子查询的效率高于连表查询
查询常用函数
UPDATE sys_user SET avatar = concat( "https://hahaha/", FLOOR(RAND()*29+1), ".png" ) WHERE avatar IS NULL OR avatar = "";
使用函数可能导致字段的索引失效,造成效率下降,建议直接读出来,然后用java进行处理
分组过滤
mysql> select name,gender from user limit 0,10; +-----------------+--------+ | name | gender | +-----------------+--------+ | 系统管理员 | 0 | | 企业管理员 | 0 | | 门店管理员 | 0 | | 郗淑 | 0 | | 柳之 | 1 | | 夏侯之 | 1 | | 吉滢 | 0 | | 独孤杰 | 1 | | 蒙飘 | 0 | | 卫柔 | 0 | +-----------------+--------+ 10 rows in set (0.00 sec) -- 根据 gender 进行分组 mysql> select name,gender from user group by gender limit 0,10; +-----------------+--------+ | name | gender | +-----------------+--------+ | 系统管理员 | 0 | | 柳之 | 1 | +-----------------+--------+ 2 rows in set (0.01 sec)
能使用where,就不要使用having,不然可能导致索引失效
SELECT语法完整结构
[]:代表可选
{}:代表必选
数据加密
【建表sql】
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` BIGINT NOT NULL auto_increment COMMENT '主键' PRIMARY KEY, `create_time` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间', `update_time` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `is_deleted` TINYINT DEFAULT 0 NULL COMMENT '是否删除 0:未删除 1:已删除', `username` VARCHAR ( 50 ) DEFAULT NULL COMMENT '用户名', `password` VARCHAR ( 255 ) NOT NULL COMMENT '密码', `gender` TINYINT DEFAULT 0 NULL COMMENT '性别 0:男 1:女', `age` INT DEFAULT NULL COMMENT '年龄' ) COMMENT '用户表'; -- 插入数据 mysql> insert into user(`username`,`password`,`gender`,`age`) values('admin','123456',0,18),('hello','123456',0,28),('word','123456',1,17); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0
【密码MD5加密操作】
mysql> select * from user; +----+---------------------+---------------------+------------+----------+----------+--------+------+ | id | create_time | update_time | is_deleted | username | password | gender | age | +----+---------------------+---------------------+------------+----------+----------+--------+------+ | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | admin | 123456 | 0 | 18 | | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | hello | 123456 | 0 | 28 | | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | word | 123456 | 1 | 17 | +----+---------------------+---------------------+------------+----------+----------+--------+------+ 3 rows in set (0.00 sec) -- 加密id=1的数据的密码 mysql> update user set password=MD5(password) where id=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from user; +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ | id | create_time | update_time | is_deleted | username | password | gender | age | +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:18:38 | 0 | admin | e10adc3949ba59abbe56e057f20f883e | 0 | 18 | | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | hello | 123456 | 0 | 28 | | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | word | 123456 | 1 | 17 | +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ 3 rows in set (0.00 sec) -- 加密所有数据的密码 mysql> update user set password=MD5(password); Query OK, 3 rows affected (0.01 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> select * from user; +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ | id | create_time | update_time | is_deleted | username | password | gender | age | +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | admin | 14e1b600b1fd579f47433b88e8d85291 | 0 | 18 | | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | hello | e10adc3949ba59abbe56e057f20f883e | 0 | 28 | | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | word | e10adc3949ba59abbe56e057f20f883e | 1 | 17 | +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ 3 rows in set (0.00 sec) -- 插入数据的时候就执行加密 mysql> insert into user(`username`,`password`,`gender`,`age`) values('md5',MD5('123456'),0,18); Query OK, 1 row affected (0.01 sec) mysql> select * from user; +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ | id | create_time | update_time | is_deleted | username | password | gender | age | +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | admin | 14e1b600b1fd579f47433b88e8d85291 | 0 | 18 | | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | hello | e10adc3949ba59abbe56e057f20f883e | 0 | 28 | | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | word | e10adc3949ba59abbe56e057f20f883e | 1 | 17 | | 4 | 2023-07-05 10:19:46 | 2023-07-05 10:19:46 | 0 | md5 | e10adc3949ba59abbe56e057f20f883e | 0 | 18 | +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+ 4 rows in set (0.00 sec)
在存储密码这种数据时,一定要进行加密,直接使用MD5加密
也还是不安全的,可以被暴力破解,可以使用更加安全的盐值加密