多列子查询

单行子查询是指查询只返回单列单行数据。

多行子查询是指子查询返回单列多行数据。

2者都是针对单列而言。

多列子查询则是指返回多列数据的子查询语句。当多列子查询返回单行数据时,在where子句中可以使用单行比较符。而返回多行数据时,在where字句中必须使用多行比较符。(in,all,any)。

[@more@]

在使用子查询比较多个列时,可以使用成对比较。即:要求多个列的数据必须同时匹配。如:在emp表中,要求在部门20中找到与部门30 薪水与补助完全相同的人:

sql>select ename,job,sal,comm,deptno from emp where (sal,comm) in (select sal,comm from emp where deptno=30) and deptno=20;

此时,where后的(sal,comm)放在括号中,子查询用sal,comm与之完全匹配。

而非成对比较,是指sal或comm,只要其中一个与部门30中的匹配即可,sql表现形式为:

sql>select ename,job,sal,comm,deptno from emp where sal in (select sal from emp where deptno=30) and comm in (select comm from emp where deptno=30) and deptno=20;

请使用浏览器的分享功能分享到微信等