mysql多表查询,子查询
一.内连接
1.关键字 INNER JOIN ON
2.示例代码:
SELECT * FROM tab1 INNER JOIN tab2 ON tab1.col1 = tab2.col1;
3.内连接简写,ON变成WHERE
SELECT * FROM tab1 , tab2 WHERE tab1.col1 = tab2.col1;
二.自连接
1.自连接因为两张表都是自己,所以需要给表起别名,否则无法写条件
2.示例代码:
SELECT tab1.col1,tab2.col1 FROM table AS tab1 , table AS tab2; WHERE tab1.col1 = tab2.col1;
三.外连接
1.分左外连接left join on和右外连接right join on
2.左外连接中会将左表中所有数据,包括不符合条件的数据都显示出来,那么不符合的数据右表没有匹配的值,就用null来表示
3.SELECT * FROM tab1 LEFT JOIN tab2 ON tab1.col = tab2.col1;
四.子查询
1.子查询的条件是另一个查询的结果,那么这个结果是需要一列的
2.示例代码:去重
DELETE FROM student
WHERE name IN (SELECT *
FROM (SELECT name
FROM student GROUP BY name
HAVING COUNT(name) > 1) AS a)
AND sid NOT IN (SELECT *FROM
(SELECT min(sid)FROM student GROUP BY name
HAVING count(name) > 1) AS b);
1.合并展示不去重
原文:https://www.cnblogs.com/memo-song/p/8982052.html