我有一个像下面这样的sql语句。如何在不使用union关键字的情况下将单行(code = 0,desc = 1)添加到此sql语句的结果中?感谢。
select code, desc
from material
where material.ExpireDate ='2010/07/23'
答案 0 :(得分: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';
答案 1 :(得分:1)
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
答案 2 :(得分:0)
由于你不想使用联合或视图,我建议在材质表中添加一个虚拟行(代码= 0,desc = 1和ExpireDate通常不会选择的东西 - 例如,1900年1月1日) - 然后使用如下的查询:
select code, desc
from material
where material.ExpireDate ='2010/07/23' or
material.ExpireDate ='1900/01/01'
通常,联盟将是我的首选。