有一天,我問了這個帖子。SQL - 回傳所有不是彼此 "相反 "的行
基本設定是,我有一個表房,像這樣創建的:
我有一個表房。
CREATE TABLE houses (
id character varying(24) NOT NULL,
bit is_nice NOT NULL.
);
還有一個更大的表,叫做house_listings,創建方法如下:
CREATE TABLE house_listings(
id character varying(24) NOT NULL,
house_one character varying(24) Not NULL,
house_two character varying(24) Not NULL,
for_sale bit NOT NULL,
PRIMARY KEY(house_one, house_two)。
FOREIGN KEY (house_one) REFERENCES houses(id)。
FOREIGN KEY (house_two) REFERENCES houses(id)
);
我正在用問題答案中建議的命令從這兩張表中提取資料:
select hl.*
from house_listings hl
where not exists (select 1)
from house_listings hl2
where hl2.house_one = hl.house_two and
hl2.house_two = hl.house_one
);
我的問題是,在這之后,我留下了一個表,如:
------------------------ ------------------- ------------------------ --------------
| id | house_one | house_two | for_sale|
------------------------ ------------------- ------------------------ --------------
| vW4eNAC7jgZVxWAGxH4xAR | 7777 | 12345 | 0x00 |
------------------------ ------------------- ------------------------ --------------
我的問題如下。有沒有辦法只從這個查詢中回傳house_one具有is_nice值為true的值?我試著將整個查詢的結果與房屋連接起來,但是SQL一直在拋出一個語法錯誤,所以這是不是需要在查詢的早期而不是后期進行?
謝謝你
uj5u.com熱心網友回復:
你應該可以在外部查詢中添加這個條件:
select hl.*
from house_listings hl join
房屋 h
on hl.house_one = h.id
where h.is_nice = 1 and
not exists (select 1)
from house_listings hl2
where hl2.house_one = hl.house_two and
hl2.house_two = hl.house_one
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/320165.html
標籤:
