我會怎麼做在SQL如下:SQL差集
select distinct(territory_id) from main_territorypricing
"minus"
select distinct(territory_id) from main_territorypricing where store_url is not null
基本上,我想包含在未包含在第二行的頂線所有territory_id
秒。
我會怎麼做在SQL如下:SQL差集
select distinct(territory_id) from main_territorypricing
"minus"
select distinct(territory_id) from main_territorypricing where store_url is not null
基本上,我想包含在未包含在第二行的頂線所有territory_id
秒。
如果你想要做的,你說:
select distinct territory_id
from main_territorypricing
where territory_id not in (
select territory_id from main_territorypricing where store_url is not null
)
但根據你所描述什麼邏輯,更容易的解決方案是:
select territory_id
from main_territorypricing
where store_url is null
這是,前提是你只有這兩個字段(territory_id和store_url),行是唯一的。
否則,還有另一個棘手的溶液:
select territory_id
from main_territorypricing
group by territory_id
having (max(store_url) is null)
你不需要內部的'DISTINCT'。減慢查詢無增益。 –
Plus:'distinct'是***不是***功能 –
Thanx @TrippKinetics和a_horse_with_no_name。 固定。 – Alisa
select distinct(territory_id) from main_territorypricing
where
territory_id in
(select territory_id from main_territorypricing)
and territory_id not in
(select territory_id from main_territorypricing where store_url is not null)
我不認爲這是回答這個問題的最好方法,但這是一個合理的答案。我不認爲這應該是downvoted。 –
的可能重複(http://stackoverflow.com/questions/8386280/minus-operator-in-mysql) – Air
[MINUS在MySQL操作者?]只要刪除引號周圍減號 – Jayvee
@Jayvee:不會幫助。 MySQL不支持'MINUS'(或標準的等效運算符'except') –