当前位置:网站首页>Gbase8s database select clause 5

Gbase8s database select clause 5

2022-06-09 20:56:00 Like to eat radish ice cold

ALL、ANY、SOME Subquery

The following example returns the order number for all orders that contain an item , The total price of this item is greater than the order number 1023 Total for each item in

price . first SELECT Use ALL Subquery , the second SELECT By using MAX The aggregate function produces the same

result .

SELECT DISTINCT order_num FROM items

WHERE total_price > ALL (SELECT total_price FROM items

WHERE order_num = 1023);

SELECT DISTINCT order_num FROM items

WHERE total_price > SELECT MAX(total_price) FROM items

WHERE order_num = 1023);

The following SELECT Statement returns the order number of all orders containing one item , The total price of this item is greater than the order number 1023 in

Total price of at least one item . first SELECT Statements use ANY keyword , And the second one. SELECT Statements use MIN

Aggregation function :

SELECT DISTINCT order_num FROM items

WHERE total_price > ANY (SELECT total_price FROM items

WHERE order_num = 1023);

SELECT DISTINCT order_num FROM items

WHERE total_price > (SELECT MIN(total_price) FROM items

WHERE order_num = 1023);

If the subquery returns exactly one value , You can omit the keyword in the subquery ANY、ALL or SOME. If you omit

ANY、ALL or SOME, And the subquery returns multiple values , You will receive an error . The subquery in the next example returns only one

That's ok , Because it uses aggregate functions :

SELECT order_num FROM items

WHERE stock_num = 9 AND quantity =

(SELECT MAX(quantity) FROM items WHERE stock_num = 9);

See also ALL、ANY and SOME Subquery .

原网站

版权声明
本文为[Like to eat radish ice cold]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206092044032859.html