简介
DQL:DataBase Query language 用来查询数据,不会对数据造成变化。把查询的结果发送到客户端进行显示。查询的结果为一个虚拟表。
1,基础查询
1,查询所有列:
2,查询指定列:select * from 表名;
select 列名1,列名2,.. from 表名;
2,条件查询
条件查询在查询的时候将条件放在where后面,where后条件的关键字有:
=(等于)
<(小于)
>(大于)
!=(不等于)
<>(不等于)
<=(小于等于)
>=(大于等于)
and (表示所有条件都必须满足)
or(表示所有条件中有一个满足就可以)
is null(为空) is not null(不为空)
between ........ and.....(表示从指定开始的值到结束的值,包含开始值和结束值)
in(set) 查找多个符合条件的内容。使用集合表示,多个值之间用逗号
not(set)查找多个不符合条件的内容。
示例:
-- 创建student表 create table student( sid int primary key, age int, name varchar(255), gender varchar(255) ) -- 1.查询年龄为15的学生 select * from student where age=15; -- 2.查询年龄不为15的学生 select * from student where age<>15; -- 3.查询年龄在25以上的学生 select * from student where age>15; -- 4.查询年龄为15且性别为男的学生 select * from student where age=15 and gender="男"; -- 5.查询年龄为15或性别为女的学生 select * from student where age=15 or gender="女"; -- 6.查询年龄不为空的学生 select * from student where age is not null; -- 7.查询年龄为空的学生 select * from student where age is null; -- 8.查询年龄在15到25的人 select * from student where agr between 15 and 25; -- 9.查询年龄为13,14,11,16的学生 select * from student where age in(13,14,11,16); -- 10.查询年龄不为 13,15,17的学生 select * from student where age in not(13,14,17);
3,模糊查询
模糊查询关键字like。
通配符:
% (百分号)代表任意字符(0-n)
- (下划线)代表一个字符
语法格式:select * from 表名 where 列名 like "匹配格式";
匹配格式:
1.%a% 查询函有a子母的内容
2.刘% 查询以刘开头的内容
3.%h 查询以h结尾的内容
4._f% 查询第二个字母为f的内容
mysql之DQL(1)
最新推荐文章于 2024-07-13 22:05:30 发布