看板 Database 關於我們 聯絡資訊
(針對 SQL 語言的問題,用這個標題。請用 Ctrl+Y 砍掉這行) 資料庫名稱: postgresql 資料庫版本: postgresql 9.6 內容/問題描述: 請問有關 postgresql 9.6 SQL 的 join 方式 資料庫 schema 如下: CREATE TABLE tbl1( id1 char(10) not null, id1_name char(12) not null, ban char(8) not null, ban_name char(60) not null ); CREATE TABLE tbl2( cust1 char(10) not null, cust1_name char(12) not null, cust2 char(8) not null, cust2_name char(60) not null ); insert into tbl1(id1,id1_name,ban,ban_name) values('A1','TEST1','12345678','COMPANY1'); insert into tbl1(id1,id1_name,ban,ban_name) values('A1','TEST1','23456789','COMPANY2'); insert into tbl1(id1,id1_name,ban,ban_name) values('A1','TEST1','34567890','COMPANY3'); insert into tbl2(cust1,cust1_name,cust2,cust2_name) values('A1','TEST1','12345678','COMPANY1'); insert into tbl2(cust1,cust1_name,cust2,cust2_name) values('A1','TEST1','23456789','COMPANY2'); 我知道 inner join 的用法我可以得到兩個 table 相同的資料如下: select * from tbl1 inner join tbl2 on id1 = cust1 and ban = cust2; id1 id1_name ban ban_name cust1 cust1_name cust2 cust2_name A1 TEST1 12345678 COMPANY1 A1 TEST1 12345678 COMPANY1 A1 TEST1 23456789 COMPANY2 A1 TEST1 23456789 COMPANY2 如果我只要取得tbl1有的資料在tbl2沒有我該如何下 sql 的 command 結果如下: id1 id1_name ban ban_name cust1 cust1_name cust2 cust2_name A1 TEST1 34567890 COMPANY3 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.235.255.55 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1662968744.A.CD9.html
konkonchou: where not exists 09/12 17:22
moyasi: Not in,no exists,left join都能做到 09/16 18:48
moyasi: 是in,no exists,left join才對 09/16 18:49
moyasi: 忽略上面 用Not in,no exists,left join這三個 09/16 18:52