必备SQL语法
一、条件查询:
1.单条件查询:
select * from info where id >1;
select * from info where id = 1;
select * from info where id >=1;
select * from info where id between 2 and 4; #取出id大于等于2,小于等于4的数据
注:*:代表所有
info:为数据表
id:代表查询列
2.多条件查询:
select * from info where name = XXX and age =12;
select * from info where name = 'XX' or age = 1;
select * from info where (name ='XX' or email ="XXXXXXX") and age = 2;
select * from info where id in (1,4.6):
select * from info where id not in (1,4,6);
select * from info where id in (select id from depart);#先查询后表中的id,然后以id为条件查询前面的表
select * from info where exists (select * from depart where id = 5); #先判断后表数据是否存在,如果存在则查询前表数据,如果不存在则前表查询不执行。
select * from info where not exists (select * from depart where id = 5);
注:info、deprt为表名
select *from (select * from info where id>2) as T where age>10;
#将 select * from info where id>2 的数据存在T的中间表中,通过 where age>10为条件查询该表。
select * from info where info.id >10;
select * from info where id >10;
注:在单表查询的时候info.id 和id无区别,但是在多表查询时,info.id用来区分需要查询的表
三、模糊查询:
select * from info where like 列名 "%黑%";#搜索表中中间为黑的数据
select * from info where like 列名 "%黑"; #搜索结尾是黑的数据(%可以代表多个字符串)
select * from info where like "_@live.com";#_:代表一个字符,__:代表两个字符。(可以添加在任何地方)
注:like为固定搭配

浙公网安备 33010602011771号