1、单行子查询
select ename,deptno,sal
from emp
where deptno=(select deptno from dept where loc='NEW YORK');
2、多行子查询
SELECT ename,job,sal
FROM EMP
WHERE deptno in ( SELECT deptno FROM dept WHERE dname LIKE 'A%');
3、多列子查询
SELECT deptno,ename,job,sal
FROM EMP
WHERE (deptno,sal) IN (SELECT deptno,MAX(sal) FROM EMP GROUP BY deptno);
4、内联视图子查询
(1)SELECT ename,job,sal,rownum
FROM (SELECT ename,job,sal FROM EMP ORDER BY sal);
(2)SELECT ename,job,sal,rownum
FROM ( SELECT ename,job,sal FROM EMP ORDER BY sal)
WHERE rownum<=5;
5、在HAVING子句中使用子查询
SELECT deptno,job,AVG(sal) FROM EMP GROUP BY deptno,job HAVING AVG(sal)>(SELECT sal FROM EMP WHERE ename='MARTIN');
SELECT t_center,t_centerN FROM k_address where T_CENTER in(SELECT r_kp FROM t_sysuser WHERE s_uname='aa')
说明:t_center(字段(int)),r_kp(字段(string))这两个相关联,但是类型不一样。
比如:t_center的值(1) r_kp的值(1,2,3)
这个模糊子查询有问题,请大家研究一下。怎么回事,说明在数据库中执行。
在.net页面上去分开执行,就可以。想想为什么?
页面执行代码:
string sql3 = "SELECT r_kp FROM t_sysuser WHERE s_uname='" + Session["userName"] + "'";
DataTable ddt1 = System.mySQLConnection.SqlConnections.GetDataTable(sql, "table");
string username = ddt1.Rows[0]["r_kp"].ToString().Trim();//1,2,3
string sql2 = "SELECT t_center,t_centerN FROM k_address where T_CENTER in ("+username+")";
DataTable ddt2 = System.mySQLConnection.SqlConnections.GetDataTable(sql2, "table");
这个就可以了,想想数据库中和页面上有什么区别?