0

我有一个如下所示的 sql 语句。如何在不使用 union 关键字的情况下将单行(code = 0,desc = 1)添加到此 sql 语句的结果?谢谢。

select code, desc
from material 
where material.ExpireDate ='2010/07/23'
4

3 回答 3

3

您始终可以为您的表创建一个视图,该视图本身使用 UNION 关键字

CREATE VIEW material_view AS SELECT code, desc, ExpireDate FROM material UNION SELECT '0', '1', NULL;
SELECT code, desc FROM material_view WHERE ExpireDate = '2010/07/23' OR code = '0';
于 2010-07-28T08:11:29.500 回答
2
 WITH material   AS
 (
 SELECT * 
 FROM 
      (VALUES (2, 'x', '2010/07/23'), 
              (3, 'y', '2009/01/01'), 
              (4, 'z', '2010/07/23')) vals (code, [desc], ExpireDate)
 )

SELECT 
     COALESCE(m.code,x.code) AS code, 
     COALESCE(m.[desc],x.[desc]) AS [desc]
FROM material m
FULL OUTER JOIN (SELECT 0 AS code, '1' AS [desc] ) x ON 1=0
WHERE m.code IS NULL OR m.ExpireDate ='2010/07/23'

code        desc
----------- ----
2           x
4           z
0           1
于 2010-07-28T12:39:40.810 回答
0

由于您不想使用联合或视图,我建议在材料表中添加一个虚拟行(代码 = 0、desc = 1 和 ExpireDate 一些通常不会被选择的东西 - 例如 01 1900 年 1 月) - 然后使用如下查询:

select code, desc
from material 
where material.ExpireDate ='2010/07/23' or 
    material.ExpireDate ='1900/01/01' 

通常,工会是我的首选。

于 2010-07-28T12:30:50.250 回答