我有一个名为stock的表。它有一个名为item_id
,'qty'
和'branch_id'
的列。
我需要做的是选择项目ID,其中'qty'列的总和大于零,branch_id的总和等于4.
我已尝试使用此查询,但我只需要查看item_id column
。这也会打印total
列。
SELECT `item_id`, SUM(`qty`) as total from `stock`
group by `item_id` HAVING total > 0 and where branch_id = 4
请帮忙。
答案 0 :(得分:0)
Select b.item_id from (
select item_id, SUM(qty) as total from stock
where branch_id = 4
group by item_id
HAVING total > 0
) as b
或此查询:
SELECT item_id FROM stock
WHERE branch_id = 4
GROUP BY item_id
HAVING SUM(qty) > 0
答案 1 :(得分:-1)
试试这个
SELECT `item_id` FROM `stock`
WHERE branch_id = 4
GROUP BY`item_id` HAVING SUM(`qty`) > 0;
GROUP BY
awlays来自WHERE
子句