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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章