我有一个表结构和值: -
used_items
id item_name qty
24 Potatoes 0
25 Potatoes 500
26 test 88
27 Wheat 3
28 abc 10
我想根据item_name诸如查询应该返回单个item_namewhere进行分组qty!=0。我用过这个查询——
SELECT * from used_items group by item_name having qty!='0'
此查询不返回 Potatoes,结果-
id item_name qty
26 test 88
27 Wheat 3
28 abc 10
预期结果-
id item_name qty
25 Potatoes 500
26 test 88
27 Wheat 3
28 abc 10
它还应该返回Potatoes包含在组中的具有非零值的 item_name。我怎样才能做到这一点?
编辑-我用过Group by是因为我想要唯一的item_name,它应该跳过item_nameif qtyis 0 for all same item_name。但如果至少一行qty是 0(例如Potatoes),则查询会跳过。此外,如果 qty 为 0,它应该item_name根据 id 升序获得匹配的行。希望我很清楚,例如;
数据:-
id item_name qty
24 Potatoes 0
25 Potatoes 500
27 Potatoes 400
28 test 88
29 Wheat 3
30 abc 10
预期结果,返回唯一item_name且第一个插入的非零值
id item_name qty
25 Potatoes 500
28 test 88
29 Wheat 3
30 abc 10