我需要顯示不匹配where子句的記錄。顯示記錄不匹配在哪裏條款
實施例 - select * from citytable where city in ('aa','bb','cc', 'dd','ee');
僅aa
,bb
,cc
存在於表格中,dd
& ee
不存在於表格中。不過,我仍然需要顯示dd
& ee
。
我需要顯示不匹配where子句的記錄。顯示記錄不匹配在哪裏條款
實施例 - select * from citytable where city in ('aa','bb','cc', 'dd','ee');
僅aa
,bb
,cc
存在於表格中,dd
& ee
不存在於表格中。不過,我仍然需要顯示dd
& ee
。
您可能正在尋找這樣的事情。 IN條件與包含IN列表中唯一(不同)值的表的內部連接相同。你想要的是一個外連接。您需要有一個表格而不是IN列表。在下面的解決方案中,我將展示如何在運行中創建此「幫助程序」表;還有其他幾種方法,這只是表明了這個想法。
select deptno, ename from emp where deptno in (10, 50, 80);
DEPTNO ENAME
------ ------
10 CLARK
10 KING
10 MILLER
with h (deptno) as (
select 10 from dual union all
select 50 from dual union all
select 80 from dual
)
select h.deptno, e.ename
from h left outer join emp e
on h.deptno = e.deptno
;
DEPTNO ENAME
------ ------
10 CLARK
10 KING
10 MILLER
50
80
我不確定你想要輸出的樣子。如果沒有城市='ee'的數據,你想要展示什麼?像這樣?
SELECT * FROM
(SELECT key AS A_key, one AS A_one, two AS A_two FROM cityTable WHERE one='aa') AS A
JOIN
(SELECT key AS E_key, one AS E_one, two AS E_two FROM cityTable WHERE one='ee') AS E
ON A_key=E_key
...etc.
編輯:也許這是它:
當我明白你說的'dd'
AMD 'ee'
不存在於表,但你仍然需要它,就可以使用union all
實現它。但請記住,「DD」和「EE」行的列將始終爲空,因爲沒有存在的記錄在你的citytable
SELECT ct.col1 AS city, ct.col2.....<all columns of your table>
from citytable ct
where city in ('aa','bb','cc')
UNION ALL
select 'dd' as city,null ,null.....<nulls as many times as the number of columns of your table>
from citytable ct1
UNION ALL
select 'ee' as city,null ,null.....<nulls as many times as the number of columns of your table>
from citytable ct2
以下應該清楚--- ************************************ ********* CityTable \t \t 市\t國家 AA \t AA BB \t BB CC \t CC ******************* ************************** \t 我的查詢 - select * from citytable where('aa','bb','cc' ,'dd','ee'); \t ********************************************* \t 輸出須─\t 市\t DD \t EE \t ************************************* ******** – Kalz
道歉的錯字.....我的輸出應該是沒有返回任何記錄在哪裏的條款.......這是dd&ee只有 – Kalz
'SELECT city FROM (SELECT city,count(*)AS c FROM cityTable GROUP BY city)WHERE c = 0'? –