sql数据库查询要点
查询
在GROUP BY 的后面使用 HAVING指定筛选条件
select * from 表 group by 分组条件 having 筛选条件
内连接INNER JOIN(只返回符合条件的值)
SELECT * FROM table1 INNER JOIN table2 ON table1.field1 = table2.field2
外连接LEFT JOIN 和RIGHT JOIN
返回全部左边表值及右表符合条件的值、返回右表全部值及左表符合条件的值
select * from a left join b on a.id = b.id(左查询)
select * from a right join b on a.id = b.id(右查询)
子查询IN(返回多条数据)
select * from 表名 where 字段名 in (子查询SQL文)
子查询比较符
返回结果为一条数据
select * from 表名 where 字段名 = (子查询SQL文)
返回结果可以为多条数据(sql为int)
select * from 表名 where 字段名 >/</<=/>= (子查询SQL文)
子查询EXISTS(返回结果为TRUE或FALSE)
子查询和父查询可不做字段关联
select * from 表名 where exists (子查询SQL文)
联合查询UNION
各个select的字段数要相同、同列字段的数据类型要兼容。
select * from A union select *from B(自动筛选重复数据)
select * from A union all select *from B(不自动筛选重复数据)