工会内部的情况?
这是我的代码:
SELECT 'Add or remove a keyword' AS 'Keyword', '1' AS 'row number'
UNION
SELECT '---------------------', '2'
UNION
SELECT
CASE
WHEN @list_item = 'duration'
THEN (SELECT duration FROM durationTable)
END
CASE
WHEN @list_item = 'location'
THEN (SELECT location FROM locationTable)
END
, '3'
ORDER BY 2
问题是,这将持续时间和位置选择为子查询,并且子查询仅限于一个结果,而且,我似乎无法找出正确的语法来将我的 CASE 提升一个级别,因此查询不是子查询。
有没有一种方法可以保留 union / select 允许的结构,同时更改最后一位查询的表,并检索整个表列而不是一个结果?
有什么建议吗?非常感谢您的浏览!
Here's my code:
SELECT 'Add or remove a keyword' AS 'Keyword', '1' AS 'row number'
UNION
SELECT '---------------------', '2'
UNION
SELECT
CASE
WHEN @list_item = 'duration'
THEN (SELECT duration FROM durationTable)
END
CASE
WHEN @list_item = 'location'
THEN (SELECT location FROM locationTable)
END
, '3'
ORDER BY 2
The trouble is, this sees the duration and location selects as subqueries, and subqueries are limited to only one result, and furthermore, I can't seem to figure out the correct syntax to bump my CASE up a level so the queries aren't subqueries.
Is there a way that I can keep the structure that the union / select allows me, while also changing up which table is queried for the last bit, and also retrieving the entire table column instead of one result?
Any tips? Thank you so much for taking a look!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,你应该知道这是一个糟糕的设计。您混淆了表示层和数据层,这会导致以后出现问题。
但是,如果您绝对致力于这样做,则可以使用附加的 SELECT 来完成:
不匹配的查询将返回空结果集并被忽略。
First, you should know this is a bad design. You are mixing up your presentation layer and your data layer and it will lead to problems down the road.
However, if you are absolutely committed to doing this, you can do it with an additional
SELECT
:The non-matching query will return an empty result set and be ignored.
这并不漂亮,但我认为这应该有效:
It's not pretty, but I think this should work: