MySQL-技術專題-連接查詢和子查詢

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"思維導圖:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/34/34820404869d10c63cbcc0e8b578980a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"連接查詢是將兩個或者兩個以上的表連接起來,從中選取所需要的數據;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"連接查詢是關係數據庫中最主要的查詢。"}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"一、內連接查詢"}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以查詢兩個或兩個以上的表"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1.查詢兩個表的所有數據,以笛卡爾積的形式展現出來"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SELECT * FROM 表1,表2;"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如:查詢t_book表和t_booktype表的內容:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3c/3ccdff40b79bc1aa39843f3602837c4e.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有5種字段,4條數據;"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_booktype;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/bb/bb18efb1268b389ab6c13902e6877413.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有2種字段,3條數據;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而查看兩個結合表的數據,則用:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book,t_booktype;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/47/47bd3cad2e0e657ac9e647905d941e3d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總共有12條數據,7種字段;"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.將兩張表的數據合成一張表(字段結合)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如,之前查詢到t_book表中的內容是:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3c/3ccdff40b79bc1aa39843f3602837c4e.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果想要在此表中加上t_booktype字段的話,就要用內連接:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book,t_booktype where t_book.bookTypeId=t_booktype.id;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"去掉bookTypeId和id字段,則有:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select bookName,price,author,bookTypeName from t_book,t_booktype where t_book.bookTypeId=t_booktype.id;\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 當然,這樣效果不是很好,讀者很難區分bookTypeName到底是t_book表中還是t_bookType表中的字段;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 這時,可以給兩個表取別名,給t_book表取別名tb,給t_booktype表取別名tby,則有:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select tb.bookName,tb.price,tb.author,tby.bookTypeName from t_book tb,t_booktype tby where tb.bookTypeId=tby.id;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0b/0ba88d2ace53baac0efcbb7f66a0f20c.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結果是一樣的,但是可以看出哪個表對應的哪個字段;"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"二、外連接查詢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"外連接查詢可以查出一張表的所有信息"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SELECT * FROM 表名1 LEFE|RIGHT JOIN 表名2 ON 表名1.屬性1=表名2.屬性2;"}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1.左連接查詢:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以查出表1的所有記錄,而表2只能查出匹配的記錄;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如:查出表1的所有記錄,加上表2的bookTypeName字段:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book left join t_bookType on t_book.Id=t_bookTypeId.id;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f3/f37e80f55b1faedbec7b0fb8a335150c.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看到t_book表的所有記錄都有,且id爲4的記錄在t_bookType表中並沒有與之對應的,因此爲null;"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.右連接查詢:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以查出表2的所有記錄,而表1只能查出匹配的記錄;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如:查出表2的所有記錄,加上表1的字段:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book left join t_bookType on t_book.Id=t_bookTypeId.id;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a3/a3d19d1fc2cb540334d58dea81cb899a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 可以看到t_bookType表的內容都查出來了,且bookypeName爲3的記錄在t_book表中沒有記錄對應,因此爲null;"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"三、子查詢"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1.帶IN關鍵字的子查詢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"若要查詢bookTypeId在t_booktype表中的數據:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book where bookTypeId in (select id from t_booktype);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/39/39e9c2afb7fbfdbbd431d3db1a8b3efc.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看出沒有bookTypeId等於4的這條數據,因爲bookTypeId等於4不在t_booktype表中;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"若要查詢bookTypeId不在t_booktype表中的數據:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book where bookTypeId not in (select id from t_booktype);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6e4a42198130672067f0d873bb5de85a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看出查到了booTypeId等於4的這條不在t_booktype表中的數據;"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.帶比較運算符的子查詢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先查看t_pricelevel表內容:select * from t_pricelevel;"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b2/b258a46cfc2e081f1ec3d00eaa729209.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查看price=80的書籍:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book where price >=(select price from t_pricelevel where priceLevel = 1);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3f/3f6d413533c5d7a93393b036a65d2104.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.帶exist關鍵字查詢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如:如果t_booktype表存在,才需要繼續查詢t_book表;"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book where exists (select * from t_booktype);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ae/ae86d73a577653d63fbea565fd28b1a8.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然,也有not exists,在前面加上NOT即可;"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.帶any的關鍵字子查詢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如:查詢t_book表中price任何一個大於t_pricelevel表中price的數據:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book where price > any (select price from t_pricelevel where priceLevel );"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6c/6ceeb5d46c9ffcef2af702637962c472.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看出t_book表中price=24的數據並沒有查出來;"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"5.帶all的關鍵字查詢"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from t_book where price> all (select price from t_pricelevel);\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c7/c7be9970159c1adc6c18eac136a30e7a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"t_book表中只有兩條數據大於t_pricelevel表中最大的價格80;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章