数据库的子查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Abel_Liujinquan/article/details/89180490
1、子查询:

使用子查询是指,在一个select语句中还嵌套着另一个select语句
示例:

select cust_id 
    from orders 
    where order_num in 
        (select order_num 
        from orderItems
        where prod_id = 'RGAN01');

注意:
作为子查询的select语句只能查询单个列,企图检索多个咧将返回错误

2、作为计算字段使用子查询

我们可以将子查询获得的值作为计算字段,示例:

select cust_name, cust_state , 
    (select count(*) 
        from orders 
        where orders.cust_id = customers.cust_id)
    as orders
    from custoomers 
    order by cust_name;

注意:
子查询最常见的使用是在where子句的in操作符中

猜你喜欢

转载自blog.csdn.net/Abel_Liujinquan/article/details/89180490