即使没有查询值也返回行

时间:2018-11-09 08:15:03

标签: sql

我在SQL中有如下查询。我想用Item返回quantity: 0,即使它不返回任何数据(即我的记录中不存在590209、590212)。我可能需要返回10000条记录。知道怎么做吗?

SELECT [Item] ,[Qty] 
  FROM Table
  where Item in (590209,590210,590211,590212,590213)

结果应该像

Item     Qty
590209   0
590210   3
590211   1
590212   0
590213   1

2 个答案:

答案 0 :(得分:0)

您可以尝试如下所示返回数量为0的“商品”

SELECT [Item] ,[Qty] 
FROM Table 
where Qty = 0

答案 1 :(得分:0)

尝试如下

select t.Item,t1.Qty from 
(
select 590209  as Item
union all
select 590210 
union all
select 590211 
union all
select 590212
 union all
select 590213
) t left join your_table t1
  on t.Item=t1.Item
相关问题