1、日期元素日期元素表表1.A=1.A=表表2.A2.ASELECT buyer_name, qtyFROM buyers CROSS JOIN salesResultResultAdam BarrAdam BarrAdam BarrAdam Barr1553711Adam Barr1003Sean Chai15Sean Chai5Sean Chai37Sean Chai11Sean Chai1003Eva Corets15Eva Corets5.Adam BarrAdam BarrAdam BarrAdam Barr1553711Adam Barr1003salessales1143231515
2、53711421003114323151553711421003114323151553711421003buyersbuyers1234Adam BarrSean ChaiEva CoretsMary OMelia1Adam Barr内联接(inner join)SELECT buyer_name, sales.buyer_id, qtyFROM buyers inner JOIN salesON buyers.buyer_id = sales.buyer_idsales114323151553711421003buyersAdam BarrSean ChaiEva CoretsMary O
3、Melia1234ResultAdam BarrAdam BarrErin OMeliaEva Corets11431553711Mary OMelia4100334114123411434外连接( Outer Joins)SELECT buyer_name, sales.buyer_id, qtyFROM buyers LEFT OUTER JOIN salesON buyers.buyer_id = sales.buyer_idsales114323151553711421003buyersAdam BarrSean ChaiEva CoretsMary OMelia12343411412
4、34ResultAdam BarrAdam BarrMary OMeliaEva Corets11431553711Mary OMelia41003Sean ChaiNULLNULL11434NULLSELECT a.buyer_id AS buyer1, a.prod_id, b.buyer_id AS buyer2FROM sales a JOIN sales bON a.prod_id = b.prod_idsales b114323151553711421003sales a1143231515537114210032315223152Result421124自联接:表自身和自身联接自
5、联接:表自身和自身联接例例: : 使用自联接查找居住在使用自联接查找居住在 California California 的的 Oakland Oakland 相同邮码区域中的作者。相同邮码区域中的作者。 USE pubsUSE pubs SELECT au1.au_fname, au1.au_lname, au2.au_fname, SELECT au1.au_fname, au1.au_lname, au2.au_fname, au2.au_lname au2.au_lname FROM authors au1 INNER JOIN authors au2 FROM authors au1 INNER JOIN authors au2 ON au1.zip = au2.zip ON au1.zip = au2.zip WHERE au1.city = Oakland WHERE au1.city = Oakland ORDER BY au1.au_fname ASC, au1.au_lname ASC ORDER BY au1.au_fname ASC, au1.au_lname ASC