一、查询语句
1、select查询一个表中的所有数据
格式:
select * from 表名 ;
案例:
select * from student ;
2、查询部分字段信息数据
格式:
select 字段1,字段2 from 表名;
案例:
select name,math from student;
3、查询字段可以用as 取别名
格式:
select 字段1 as "别名名称",字段2 " 别名名称2" from 表名 ;
备注:as也可以省略不写
案例:
select name as "姓名",math " 数学" from student2 ;
4、where接条件指定查询内容
where +条件
条件:比较运算符
,<,=,!=,<>,>=,<=
案例:
(1)=等于
select * from student where id=2;
(2)a、!=不等于
select * from student where id!=2;
b、<>不等于
select * from student where id<>2;
(3)大于
select * from student where id>2;
(4)小于
select * from student where id<2;
(5)大于等于
select * from student where id>=2;
(6)小于等于
select * from student where id<=2;
(7)and 同时满足
select * from student where id>3 and math>90;
(8)or 满足其中一个条件就能查询出数据
select * from student where id>6 or math>97;
(9)beteen ... and ... 在什么范围之间
select * from student where id BETWEEN 2 and 5;
(10)in 在一组数据中匹配
select * from student where id in (2,8,6)
(11)not in 不在一组数据中匹配
select * from student where id not in (2,8,6)
(12)is null
select * from student where class is null
(13)is not null
案例:select * from student where class is not null
5、排序:order by
(1)升序:asc 可以省略不写
select * from student order by math asc ;
(2) 降序:desc 降序
select * from student order by math desc ;
(3)二次排序
select * from student2 order by math desc ,age desc;
6、like 模糊查询
%:%是通配符
:代表一个字符
(1)查找8开头的数据
select * from student where english like "8%"
(2)查找包含8的数据
select * from student2 where math like "%8%"
(3)查找8结尾的数据
select * from student2 where math like "%8"
(4)查看指定字符的数值
select * from student where english like "8__"
7、limit 查询指定的数据,限制
根据索引来取值,从0开始,一个表的第一行就是0,第二数字,行数(3行)
案例:
select * from student limit 0,3
即为从第0行开始,查看0行的后3行,显示出第一行,第二行,第三行数据。