我正在尝试使用这些查询为查询结果添加默认值“选择餐厅” ;
SELECT * FROM(
SELECT No_,Name + ' - ' + No_ Name FROM [dbo].[TestDB]
UNION
SELECT '<Choose a Restaurant>' AS Name) A
WHERE [Alt Grup] = 'TAB' or [Alt Grup] = 'FRC' or [Alt Grup] = 'KIB' or [Alt Grup] = 'ORK')
and (Blocked = 0)
and ([Chain Name] = '01-TAB' or [Chain Name] = 'YURT DIŞI' or [Chain Name] = 'FAS')
and [Show On Web] = 1
and No_ != '2001.10.01.000998'
ORDER BY Name
但是,它不会成功。我做错了什么?预先感谢。
编辑:这是我编辑原始查询以强制Union
;
select No_,Name + ' - '+No_ Name, 'Choose a Rest' from [dbo].[TestDB]
where ([Alt Grup] = 'TAB' or [Alt Grup] = 'FRC' or [Alt Grup] = 'KIB' or [Alt Grup] = 'ORK')
and (Blocked = 0)
and ([Chain Name] = '01-TAB' or [Chain Name] = 'YURT DIŞI' or [Chain Name] = 'FAS')
and [Show On Web] = 1
and No_ != '2001.10.01.000998'
order by Name
答案 0 :(得分:0)
您在第一次选择时有错误的列名顺序(似乎是两列,但是 No_名称似乎有误)
No_, Name + ' - ' + No_ Name FROM [dbo].[TestDB]
对于UNION,两个选择中都需要相同的编号和相应的数据类型列,因此您可以尝试
SELECT *
FROM(
SELECT 'No_' + Name + ' - ' + No_ + Name AS name
FROM [dbo].[TestDB]
UNION
SELECT '<Choose a Restaurant>'
) A
ORDER BY Name
,您可以在WHERE子句中仅使用表中存在的列名(子查询()A) 你有 因此,您只能过滤名称..
的值答案 1 :(得分:0)
您的查询有几个问题。我会按照这些思路将其重组为更多内容。
SELECT No_
, Name + ' - ' + No_ Name
FROM [dbo].[TestDB]
WHERE [Alt Grup] in ('TAB', 'FRC', 'KIB', 'ORK')
and Blocked = 0
and [Chain Name] in ('01-TAB', 'YURT DIŞI', 'FAS')
and [Show On Web] = 1
and No_ != '2001.10.01.000998'
UNION ALL
SELECT NULL
, '<Choose a Restaurant>' AS Name
ORDER BY Name