一.MySql基础查询语句
1. order by 排序
select m* from a order by id;
以id字段进行排序
select * from a order by 2;
以第二个字段进行排序,这里只有两个字段只能:
order by 1
order by 2
如果order by 3超过3个字段就会报错,应为这里只有两个字段
desc倒序
select * from a order by id desc;
2. limit n,m(分页)
n表示从第几行开始,m表示取第几条
select*from a limit 0,1
从第一条数据进行分页,取第一条数据
两行数据
四行数据
select*from a limit 1,4;
从第二条数据开始,输出4条数据,因为是从第二条开始的所以只显示了 三条数据
3.模糊查询
like(跟等于差不多)
select * from a where xm like ‘xiaoming’;
查询相似xiaoming的数据
‘%%’ %号等于任意字符
select * from a where xm like ‘%x%’;
查询数据里面为x的数据,x%表示查询带有开头x的数据,%x表示查询结尾带有x的数据
4.运算符号
(+ - * / %)
5.逻辑运算
AND(&)与
OR (|)或
NOT (!)非
selectfrom a where xm=‘hack.hello’ and id=3;
查询xm为hack.hello的数据且id=3
要必须要满足两个条件
selectfrom a where xm=‘hack.hello’ or id =2;
查询xm字段带有hack.hello或id带2的数据
只要满足一个条件即可
selectfrom a where not xm=‘hack.hello’
查询xm字段里面不为‘hack.hello’的数据
6.sleep()延时
select sleep(5)
延时5秒回显
二.联合查询
union将两个表里的数据合到一起输出
selectfrom a where id=3 union select 1,2;三.子查询
select*from a where xm=(select ‘hack.hello’);