如何从2个查询的组合中获取有限数量的行

时间:2013-02-22 09:47:15

标签: timesten

Iam面临一些逻辑问题。希望有人能找到我的解决方案

我有查询

select FirstName||'|'||LastName||to_char(salary) 
from (select FirstName,LastName,salary from subs_tbl where salary=24000) 
union all select FirstName||'|'||''||to_char(salary) 
from (select FirstName,LastName,salary from subs_tbl where salary=30000)

所以我的结果将是

Satish|Kumar|24000

Vimal|Kumar|24000

Dinesh||30000

假设我有3行作为我的结果,但我需要在整个查询中添加另一个条件,我只需要这些查询组合中的2行。

请帮我解决这个问题

2 个答案:

答案 0 :(得分:1)

试试这个避免使用的联盟

SELECT FirstName||'|'||LastName||to_char(salary) 
FROM subs_tbl 
WHERE salary=24000 or salary=30000) 
LIMIT 2

修改

SELECT fullname
FROM   (SELECT firstname
           || '|'
           || lastname
           || To_char(salary) AS 'FullName'
    FROM   (SELECT firstname,
                   lastname,
                   salary
            FROM   subs_tbl
            WHERE  salary = 24000)
    UNION ALL
    SELECT firstname
           || '|'
           || ''
           || To_char(salary) AS 'FullName'
    FROM   (SELECT firstname,
                   lastname,
                   salary
            FROM   subs_tbl
            WHERE  salary = 30000))
WHERE  rownum <= 5;

希望这有帮助

答案 1 :(得分:1)

select FirstName||'|'||LastName||to_char(salary) 
from (select FirstName,LastName from subs_tbl where salary=24000) 
union all select FirstName||'|'||''||to_char(salary) 
from (select FirstName,LastName from subs_tbl where salary=30000) limit 0,2