带有条件选择的子查询

时间:2013-05-16 12:59:47

标签: mysql

编辑尝试列出数量大于同一作业的任何其他商品的所有商品

Table items
+--------+----------+
| job_id | quantity | 
+--------+----------+
| 004    |      150 |
| 004    |        4 |
| 004    |      100 |
| 002    |       50 |
| 002    |        2 |
| 002    |       17 |
| 002    |       17 |
| 006    |        2 |
+--------+----------+

我知道它应该是一个相当简单的但是它让我卡住了。我试图在查询中使用ANY,但我不太确定如何使用它。这就是我的想法:

select job_id, quantity
from items
where quantity >
    (select min(quantity) from items) group by job_id;

我离开了那个。谢谢你的帮助

select job_id, max(quantity) from po_items group by job_id;

那个人做了伎俩

4 个答案:

答案 0 :(得分:1)

请改为尝试:

select job_id, quantity 
from items 
where quantity > (select MAX(quantity) from items where ...);

答案 1 :(得分:0)

您可以使用功能MAX()

您可以使用select MAX(quantity) from items where

select job_id, quantity 
from items 
where quantity > (select MAX(quantity) from items where ...);

答案 2 :(得分:0)

PSR走在正确的道路上。

对于每个作业的最大值,可以使用聚合MAX()函数,最有效地使用GROUP BY。

尝试:

select job_id, MAX(quantity) from items GROUP BY job_id

(从手机发送)

答案 3 :(得分:0)

不使用SUBQUERY。请检查SQLFIDDLE

SELECT
  job_id,
  MAX(quantity)
FROM items
GROUP BY job_id
相关问题