【发布时间】:2014-03-01 17:05:08
【问题描述】:
到最基本的查询
SELECT id, column1, column2
FROM table1
WHERE id IN ("id1", "id2", "id3")
其中 where 语句中的参数作为变量传递,我还需要为不存在 id 的行返回值。一般来说,这是一个非常相似的问题,如下所述:SQL: How to return an non-existing row? 但是,WHERE 语句中有多个参数
id2不存在时的结果:
-------------------------------
| id | column1 | column2 |
-------------------------------
| id1 | some text | some text |
| id3 | some text | some text |
-------------------------------
id2 不存在时的期望结果
-----------------------------------
| id | column1 | column2 |
-----------------------------------
| id1 | some text | some text |
| id2 | placeholder | placeholder |
| id3 | some text | some text |
-----------------------------------
我的第一个想法是创建一个临时表并将其连接到查询中。不幸的是,我无权创建任何类型的临时表,因此我只能使用 SELECT 语句。
有没有办法在 SQL SELECT 查询中做到这一点?
编辑: 确实,上述是一种假设情况。在 WHERE 子句中可以是数百个 id,其中缺失的数量未知。
【问题讨论】:
-
@MrJack 显然不是!