当使用比较运算符连接子查询与父查询时,要求子查询必须返回单个值。如果子查询返回多个值,则要用 ANY 或 ALL 谓词修饰比较运算符。ANY 的含义是“某个,其中之一”,ALL 的含义是“所有,全部”。表 1.15 列出了 ANY 和 ALL 与 6 种比较运算符搭配而构成的 12 个谓词。
例 1.19 查询比 101 顾客的任一订单的付款日期长的顾客的订单号及付款日期:
select order_num,customer_num,paid_date
from orders
where paid_date>any(select paid_date
from orders
where customer_num=101)and customer_num<>101;
查询结果为:
order_num customer_num paid_date
1003 104 /06/14/2008
1004 102 /07/21/2008
1005 104 /07/31/2008
执行此查询时,首先处理子查询,找出 101 顾客的所有订单的付款日期,构成一个集合(付款日期为“07/22/2014”和“06/03/2014”),然后处理父查询,查找大于此集合中任意一个数值的其他订单、客户号及付款日期。
例 1.20 查询比 101 顾客的全部订单的付款日期都要长的顾客的订单号及付款日期:
select order_num,customer_num,paid_date
from orders
where paid_date>all(select paid_date
from orders
where customer_num=101)and custo