mysql 嵌套使用
1. SELECT语句的子查询:
语法:SELECT ... FROM 表1 WHERE 列1 =
(SELECT ... FROM ... WHERE ....)
2.SELECT语句的表查询
语法:SELECT ... FROM (SELECT....) AS TEMP_TABLE WHERE ...
注意:这里面有个注意,就是列名不能重复!有些人会出现:#1060 - Duplicate column name '列名' 错误,这个就是列名重复了
解决方案:去掉一个列名。
PS:这个和MSSQL别名有点不一样,
select * from (SELECT * FROM a) b
3.条件关联关系查询:
语法:便于查看我缩进下代码
SELECT col1 FROM t1 AS a
WHERE a.col1 = (SELECT col1 FROM b AS a
WHERE a.col1 = ( SELECT col1 FROM c
WHERE a.col2 = c.col1
)
);
4.SELECT (SELECT s1 FROM t2) FROM t1;
5.SELECT (SELECT s2 FROM t1);