php 怎么用sql语句查询子级的子级

php 怎么用sql语句查询子级的子级,第1张

嵌套SELECT语句也叫子查询,一个 SELECT 语句的查询结果能够作为另一个语句的输入值。子查询不但能够出现在Where子句中,也能够出现在from子句中,作为一个临时表使用,也能够出现在select list中,作为一个字段值来返回。

1、单行子查询 :单行子查询是指子查询的返回结果只有一行数据。当主查询语句的条件语句中引用子查询结果时可用单行比较符号(=, >, <, >=, <=, <>)来进行比较。 例:

select ename,deptno,sal from emp

where deptno=(select deptno from dept where loc='NEW YORK')

2、多行子查询:多行子查询即是子查询的返回结果是多行数据。当主查询语句的条件语句中引用子查询结果时必须用多行比较符号(IN,ALL,ANY)来进行比较。其中,IN的含义是匹配子查询结果中的任一个值即可("IN" *** 作符,能够测试某个值是否在一个列表中),ALL则必须要符合子查询的所有值才可,ANY要符合子查询结果的任何一个值即可。而且须注意ALL 和ANY *** 作符不能单独使用,而只能与单行比较符(=、>、<、>= 、<= 、<>)结合使用。 例:

1).多行子查询使用IN *** 作符号例子:

查询选修了老师名叫Rona(假设唯一)的学生名字

sql>select stName from Student

where stId in(selectdistinct stId from score where teId=(select teId from teacher where teName='Rona'))

查询所有部门编号为A的资料: SELECT ename,job,sal FROM EMP

WHERE deptno in ( SELECT deptno FROM dept WHERE dname LIKE 'A%')

2).多行子查询使用ALL *** 作符号例子:查询有一门以上的成绩高于Kaka的最高成绩的学生的名字: sql>select stName from Student

where stId in(select distinct stId from score where score >all(select score from score where stId=(select stId from Student where stName= 'Kaka') ))3). 多行子查询使用ANY *** 作符号例子:查询有一门以上的成绩高于Kaka的任何一门成绩的学生的名字:

sql>select stName from Student

where stId in(select distinct stId from score where score >any(select score from score where stId=(select stId from Student where stName='Kaka')))

希望能帮到你

直接上代码

[php] view plain copy

$product_info = M('product_info as info')

[php] view plain copy

$productinfo = $product_info->join('left join c_price_comment as comment on info.sku=comment.sku')->where($where)->order('info.create_time desc')->field('info.*,comment.comment_count')->buildSql()

$list = M()->table($productinfo.' a')->order(' '.$order.'')->group('sku ')->limit($limit)->select()

buildSql方法后不会进行实际的查询 *** 作,而只是生成该次查询的SQL语句


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6723710.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-27
下一篇2023-03-27

发表评论

登录后才能评论

评论列表(0条)

    保存