看板 Database 關於我們 聯絡資訊
我提以下的寫法, 或許看起來不怎麼smart, 但測試過 確定能run, 結果也符合預期. select type, count(*), sum(cost) from tbl where ID in(select ID from tbl where type='A') and ID in(select ID from tbl where type='B') group by type; ※ 引述《KnightX (........)》之銘言: : 昨天被板主誤刪, 容我再 po 一次... Orz : 想請教一下各位前輩, 我用的資料庫是 MySQL, : 假設我有一個資料表如下: : no ID type cost : ---------------------- : 1 123 A 100 : 2 123 B 150 : 3 445 B 150 : 4 445 B 150 : 5 677 A 100 : 6 677 B 150 : 7 677 B 150 : . . . . : . . . . : ---------------------- : 我要看不同 type 的摘要資訊的話, 只要打 : select type, count(*), sum(cost) : from table : group by type; : 但現在我想要過濾掉同一個 ID 下只有一種 type 的資料, : 以上面的表來說就是踢掉 ID 為 445 的兩筆資料, : 因為他的 type 就只有一個 B 而已, : 也就是想要只保留同一個 ID 下至少有兩種 type 的資料~! : 不知道像這樣的情形 SQL 語法可以做到嗎?? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.104.186.105
KnightX:感謝 bobju 提供的方法!我都忘了可以土法煉鋼硬做... Orz 04/23 11:30