工会内部的情况?

发布于 2024-12-15 05:31:15 字数 543 浏览 1 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七月上 2024-12-22 05:31:15

首先,你应该知道这是一个糟糕的设计。您混淆了表示层和数据层,这会导致以后出现问题。

但是,如果您绝对致力于这样做,则可以使用附加的 SELECT 来完成:

...
UNION

SELECT duration FROM durationTable, '3'
WHERE @list_item = 'duration'

UNION

SELECT location FROM locationTable, '3'
WHERE @list_item = 'location'
...

不匹配的查询将返回空结果集并被忽略。

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:

...
UNION

SELECT duration FROM durationTable, '3'
WHERE @list_item = 'duration'

UNION

SELECT location FROM locationTable, '3'
WHERE @list_item = 'location'
...

The non-matching query will return an empty result set and be ignored.

风吹过旳痕迹 2024-12-22 05:31:15

这并不漂亮,但我认为这应该有效:

SELECT 'Add or remove a keyword' AS 'Keyword', '1' AS 'row number'
UNION
SELECT '---------------------', '2'
UNION
SELECT duration, '3' FROM durationTable WHERE 1 = (SELECT CASE WHEN @list_item = 'duration' THEN 1 ELSE 0 END)
UNION
SELECT location, '3' FROM locationTable WHERE 1 = (SELECT CASE WHEN @list_item = 'location' THEN 1 ELSE 0 END)

It's not pretty, but I think this should work:

SELECT 'Add or remove a keyword' AS 'Keyword', '1' AS 'row number'
UNION
SELECT '---------------------', '2'
UNION
SELECT duration, '3' FROM durationTable WHERE 1 = (SELECT CASE WHEN @list_item = 'duration' THEN 1 ELSE 0 END)
UNION
SELECT location, '3' FROM locationTable WHERE 1 = (SELECT CASE WHEN @list_item = 'location' THEN 1 ELSE 0 END)
红尘作伴 2024-12-22 05:31:15
SELECT 'Add or remove a keyword' AS 'Keyword', '1' AS 'row number'
UNION
SELECT '---------------------', '2'
UNION
SELECT duration,3 FROM durationTable WHERE @list_item = 'duration'
UNION
SELECT location,3 FROM locationTable WHERE @list_item = 'location'
SELECT 'Add or remove a keyword' AS 'Keyword', '1' AS 'row number'
UNION
SELECT '---------------------', '2'
UNION
SELECT duration,3 FROM durationTable WHERE @list_item = 'duration'
UNION
SELECT location,3 FROM locationTable WHERE @list_item = 'location'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文