Re: [SQL ] mysql如何交集查詢條件?

看板Database作者 (TeemingVoid)時間10年前 (2014/01/21 22:39), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串4/4 (看更多)
※ 引述《SonEat (善液)》之銘言: : 再追加一個差集的問題 : 我把原表再多一欄 : table:MoleculeElement : MoleculeID Element Number : 1 Bi 1 : 1 Na 3 : 2 Bi 1 : 2 Na 1 : 2 O 1 : 3 Bi 3 : 3 C 1 : create table MoleculeElement : ( : MoleculeID int, : Element varchar(2), : Number int : ); : insert into MoleculeElement values : (1,'Bi',1),(1,'Na',3), : (2,'Bi',1),(2,'Na',1),(2,'O',1), : (3,'Bi',1),(3,'C',3); : : 我想查詢同時有Na與Bi的結果,也就是原問題1,再排除Na的Number=3的結果 : 也就是查詢結果只得到 2 select MoleculeID from ( select MoleculeID, Element from MoleculeElement where Element = 'Bi' ) as A join ( select MoleculeID, Element from MoleculeElement where Element = 'Na' ) as B using (MoleculeID) where MoleculeID not in ( select MoleculeID from MoleculeElement where Element = 'Na' and Number = 3 ) -- 或者 -- select MoleculeID from ( select MoleculeID, Element from MoleculeElement where Element = 'Bi' ) as A join ( select MoleculeID, Element from MoleculeElement where Element = 'Na' and Number <> 3 ) as B using (MoleculeID) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.41.97.177

01/21 23:34, , 1F
再次感謝大大指教
01/21 23:34, 1F
文章代碼(AID): #1IteQWvl (Database)
文章代碼(AID): #1IteQWvl (Database)