2

考虑下表

桌子

SELECT QUEUENAME, 
       PRIORITY 
  FROM QUEUES 
 WHERE QUEUENAME IN ('Windows','Macintosh')

从上面的查询和表值来看,没有QueueNameas "Macintosh"。因此,我希望只要 where 子句中没有匹配的值,就应该选择一个带有QueueName “未知”的值及其值。

请告诉我如何实现这个查询?

我正在寻找的输出是

| QUEUENAME | PRIORITY |
|-----------|----------|
|   Windows |        1 |
|   Unknown |        3 |
4

3 回答 3

2

更新你在寻找这样的东西吗?

SELECT COALESCE(q.queuename, q2.queuename) queuename,
       COALESCE(q.priority, q2.priority) priority
  FROM 
(
  SELECT 'Windows' queuename FROM dual
  UNION ALL
  SELECT 'Macintosh' FROM dual
) s LEFT JOIN queues q
    ON s.queuename = q.queuename JOIN queues q2
    ON q2.queuename = 'Unknown'

输出:

| 队列名 | 优先级 |
|-----------|----------|
| 窗户 | 1 |
| 未知 | 3 |

这是SQLFiddle演示 (Oracle)
这是SQLFiddle演示 (SQL Server)

于 2013-08-29T09:12:03.530 回答
0
create table queues as
select 'Windows' as queuename, 1 as priority from dual union all
select 'Linux'   as queuename, 2 as priority from dual union all
select 'Unknown' as queuename, 3 as priority from dual
;

with data_(qname) as (
select 'Windows'   from dual union all
select 'Macintosh' from dual
)
select coalesce(queues.queuename, default_.queuename) as queuename,
       coalesce(queues.priority, default_.priority) as priority
from data_
left outer join queues on queues.queuename = data_.qname
inner join queues default_ on default_.queuename = 'Unknown'
;

QUEUENA   PRIORITY
------- ----------
Windows      1
Unknown      3
于 2013-08-29T09:19:40.987 回答
0

这是一个查询

select * from queues where queuename in ('Windows','Macintosh')
UNION ALL
select * from queues where queuename in 
(
select decode
(
(
select count(queuename) from queues 
where queuename NOT IN  ('Windows','Macintosh')
),1,NULL,'Unknown')
queuename from queues where queuename NOT in ('Windows','Macintosh'))

该查询给出了您的预期结果

如果你给 windows Macintosh 它给 windows unknown。如果你给 linux macintosh 它给 linux unknown 如果你给 windows unknown 它给 windows unknown 如果你给 Linux unknown 它给 linux unknown 如果你给 linux windows 它给 linux windows

我只在过去 4 个月内为 oracle 编写代码,所以一些有经验的人可以提供比这更简单的代码......

你必须在 3 个地方给出位置

于 2013-08-29T10:07:42.717 回答