语法:xxxxxxxxxx select 字段列 from 表[ where 字句 ][ group by 分组依据字段列 ]having 筛选条件[ order by 排序依据 asc|desc ][ limit N ]
单表的搜索判断查询
1.if()函数
语法:select *,if(score>60,'及格','不及格') as 情况 from grade
2.case when end 搜索语法
case 字段
when 值1 then 结果1
when 值2 then 结果2
when 值3 then 结果3
....
else 默认结果
end as 新列名
case
when 字段 逻辑符号 值1 then 结果1
when 字段 逻辑符号 值2 then 结果2
....
else 默认结果
end as 别名
多表连接
1.内连接
select 字段列
from 表1 inner join 表2 on 连接依据 [表1.字段 = 表2.字段]
select 字段列
from 库名.表1 inner join 库名.表2 on 连接依据 [库名.表1.字段 = 库名.表2.字段]
2.外连接
1.左连接
select 字段列
from 表1 left join 表2
on 连接依据 [表1.字段 = 表2.字段]
2.右连接
select 字段列
from 表1 right join 表2
on 连接依据 [表1.字段 = 表2.字段]
3.全外连接
select * from 表1 left join 表2 on 表1.字段=表2.字段
union
select * from 表1 right join 表2 on 表1.字段=表2.字段;
复合条件链接查询
子查询--带条件运算符的查询
select * from 表1 where 售价 >(
select 售价 from 表2 where name="XXX"
)
子查询--带in的子查询
select * from commodity where c_type in (
select ct_id from commoditytype where ct_name in ("玩具","文具")
)
相当于
select * from commodity where c_type in (1,2) -- 其中in 中的id号 由子查询获取
子查询 ---逻辑运算符 配合 any / all 进行条件链接
select * from commodity
where c_inprice >any(
select distinct c_inprice from commodity where c_type=1)
and c_type!=1