我有两列,一列是type,另一列是id,我想获取所有类型的id为null的所有记录,但type ='Started'除外,这意味着当type为'Started'时应显示记录而不检查ID,在其他类型中,它将仅显示ID为null的那些记录。有帮助吗?
答案 0 :(得分:3)
我认为您的意思是:
WITH your_table AS (SELECT 1 ID, 'started' TYPE FROM dual UNION ALL
SELECT NULL ID, 'finished' TYPE FROM dual UNION ALL
SELECT 2 ID, 'finished' TYPE FROM dual UNION ALL
SELECT NULL ID, 'started' TYPE FROM dual UNION ALL
SELECT 3 ID, 'started' TYPE FROM dual UNION ALL
SELECT 4 ID, 'progressing' TYPE FROM dual)
SELECT *
FROM your_table
WHERE (ID IS NULL AND TYPE != 'started')
OR TYPE = 'started'
ORDER BY TYPE, ID;
ID TYPE
---------- -----------
finished
1 started
3 started
started
答案 1 :(得分:2)
我认为where子句就可以了:
where type = 'Started' or (type <> 'Started' and id is null)
此外,此查询属于SQL而不是PL / SQL(这是一种程序语言)。