mysql新手入门随笔4

40、子查询:出现在其他SQL语句里的SELECT语句

例如:SELECT sname,mark FROM student WHERE mark = (SELECT max(mark) FROM student);

SELECT sname,mark FROM student WHERE mark in(SELECT max(mark) FROM student);

等效于:SELECT sname,mark FROM student WHERE mark =any(SELECT max(mark) FROM student);

in等效于=any

not in等效于!=all<>all

41、多表连接查询

内连接:INNER JOIN,JOIN,CROSS JOIN,NATURAL JOIN

意思:两张表的交集,仅显示符合连接条件的记录,不符合的不显示

用法:SELECT a.*,b.* FROM pa a INNER JOIN pb bON a.id = b.id;

()连接:LEFT (OUTER) JOIN

意思:显示左表全部和左右表中符合连接条件的记录

用法:SELECT a.*,b.* FROM pa a LEFT (OUTER)JOIN pb bON a.id = b.id;

()连接:RIGHT (OUTER) JOIN

意思:显示右表全部和左右表中符合连接条件的记录

用法:SELECT a.*,b.* FROM pa a RIGHT (OUTER)JOIN pb bON a.id = b.id;

MySQL里的全连接

SELECT a.*,b.* FROM pa a LEFT (OUTER)JOIN pb bON a.id = b.id

UNION

SELECT a.*,b.* FROM pa a RIGHT (OUTER)JOIN pb bON a.id = b.id;

UNION:会对结果去重

UNION ALL:不会对结果去重

相关推荐