{"id":149380,"date":"2020-08-23T17:16:05","date_gmt":"2020-08-23T09:16:05","guid":{"rendered":"http:\/\/4563.org\/?p=149380"},"modified":"2020-08-23T17:16:05","modified_gmt":"2020-08-23T09:16:05","slug":"sql-%e9%97%ae%e9%a2%98%e8%af%b7%e6%95%99%ef%bc%88mysql%ef%bc%89","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=149380","title":{"rendered":"SQL \u95ee\u9898\u8bf7\u6559\uff08mysql\uff09"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  SQL \u95ee\u9898\u8bf7\u6559\uff08mysql\uff09               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CUMTProgrammer <\/span>  <span><i><\/i> 11<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">  <\/p>\n<pre><code>select a.id, c.name as status ,d.name as type  from a  left join b c on a.status =c.id left join b d on a.type =d.id <\/code><\/pre>\n<pre><code>select a.id, (selct name from b where a.status = b.id) as status, (selct name from b where a.type = b.id) as type from a <\/code><\/pre>\n<p>a \u8868\u4e0e b \u8868\u5173\u8054\uff0cb \u4f5c\u4e3a\u5b57\u5178\u8868\uff0c\u5b58\u7740\u540d\u79f0\u7b49\u4fe1\u606f\uff0ca \u53ea\u6709 id \u3002<\/p>\n<p>\u4e0d\u8ba8\u8bba\u8fd9\u4e2a\u8868\u7ed3\u6784\u6539\u53d8\u7b49\u7b49\uff08\u4ee5\u53ca\u7528\u4e86\u5f88\u4e45\uff0c\u6570\u636e\u5f88\u591a\uff0c\u800c\u4e14\u4e0d\u662f\u6211\u80fd\u51b3\u5b9a\u7684\uff09\uff0c\u4ec5\u4ece SQL \u89d2\u5ea6<\/p>\n<p>1.\u4e24\u79cd\u5199\u6cd5\u6211\u90fd\u89c9\u5f97\u5f88\u4e11\u964b\uff0c\u6709\u6ca1\u6709\u66f4\u597d\u7684<\/p>\n<p>2.\u5982\u679c\u6ca1\u6709\u7684\u8bdd\uff0c\u54ea\u4e00\u79cd\u7a0d\u5fae\u597d\u4e00\u70b9<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3123561\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : youzipi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u5fc5\u8981 join \u4e24\u6b21\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123562\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luojianxhlxt <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3a\u5565\u8981 join \u4e24\u6b21\u5440<br \/>\u867d\u7136\u6211\u90fd\u662f a.status =c.id(+)\u8fd9\u4e48\u5199<\/p>\n<p>\u8fd9\u4e24\u4e2a\u6765\u8bf4\uff0c\u7b2c\u4e00\u4e2a\u597d\u5427                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123563\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : CUMTProgrammer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @luojianxhlxt <br \/>@youzipi <br \/>\u5982\u679c join \u4e00\u6b21\u7684\u8bdd\uff0c\u540e\u9762\u7684 type \u8981\u600e\u4e48\u67e5\u8be2?\u80fd\u5426\u5199\u4e00\u4e0b\u5b8c\u6574\u7684 sql<br \/>\u4e24\u4e2a\u5c5e\u6027\u5e76\u4e0d\u662f\u4e00\u884c\u6570\u636e\uff0ctype \u662f\u4e00\u884c\uff0cstatus \u662f\u4e00\u884c\uff0c\u662f\u5b57\u5178\u8868\u7684\u5f62\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123564\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : CUMTProgrammer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u662f\u67e5\u8be2\u4e00\u884c\u7684\u8bdd\uff0c\u6211\u4e5f\u89c9\u5f97 join \u8981\u597d\uff0c\u4f46\u662f\u8fd9\u4e2a\u662f a \u7684\u591a\u4e2a\u5c5e\u6027\u90fd\u8981\u4ece b \u83b7\u53d6\uff0c\u53ef\u4ee5\u7b80\u5355\u7684\u7406\u89e3 b \u53ea\u6709 id,name \u4e24\u4e2a\u5b57\u6bb5\uff0ca \u5b58\u7740 b \u7684 id                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123565\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bixchen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e00\u79cd\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123566\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : redtea <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a\u5b57\u5178\u8868\u5168\u8868\u67e5\u51fa\u6765\uff0c\u5199\u5165\u7f13\u5b58\uff0c\u7b49\u6709\u6570\u636e\u66f4\u65b0\u4e86\u518d\u66f4\u65b0\u7f13\u5b58\u3002<br \/>\u53ea\u8981\u67e5\u8be2 a \u8868\u5c31\u884c\u4e86\uff0cstatus \u548c type \u5bf9\u5e94\u7684\u540d\u79f0\u4ece\u7f13\u5b58\u67e5\u3002<br \/>\u5982\u679c\u5b57\u5178\u8868\u5e38\u5e74\u4e0d\u53d8\u7684\u8bdd\uff0c\u4f5c\u4e3a\u5e38\u91cf\u90fd\u884c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123567\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : justgodlike1993 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             WHERE a.type = c.id?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123568\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lovecy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             #6 \u6b63\u89e3\uff0c\u975e\u8981 SQL \u5c31\u7b2c\u4e00\u79cd\u4e86\uff0c\u522b\u7ea0\u7ed3\u4e86\uff0c\u5934\u53d1\u8981\u7d27                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123569\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : realpg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ece\u8bed\u4e49\u903b\u8f91\u4e0a\u770b\u4e5f\u662f left join \u6ca1\u5565\u6027\u80fd\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123570\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guanhui07 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e5f\u89c9\u5f97 \u4e00\u6b21 join                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123571\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : taogen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u5e94\u8be5\u6ca1\u6709\u3002<br \/>2. \u4e60\u60ef\u7b2c\u4e00\u79cd\u5199\u6cd5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123572\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhangysh1995 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e2a\u4eba\u89c9\u5f97\u7b2c\u4e8c\u79cd\u66f4\u4f18\u96c5\u3002a.type, a.id, b.id \u5982\u679c\u6709\u7d22\u5f15\uff0c\u652f\u6301\u7b2c\u4e8c\u79cd\uff0c\u901f\u5ea6\u5e94\u8be5\u4f1a\u66f4\u5feb\u3002<\/p>\n<p>\u7b2c\u4e00\u79cd\u7684\u8bdd left join \u4e00\u6b21\u5c31\u597d\uff0c `xxx on a.status = b.id and a.type = b.id`, \u9700\u8981\u52a0\u7d22\u5f15 (status, type), \u6ce8\u610f predicate \u987a\u5e8f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3123573\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : CUMTProgrammer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zhangysh1995 \u5144\u5f1f\uff0c\u4f60\u7684 join \u4e00\u6b21\u5199\u6cd5\u592a\u60f3\u5f53\u7136\u4e86\u3002\u4f60\u53ef\u4ee5\u81ea\u5df1\u8bd5\u4e00\u6b21                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>SQL \u95ee\u9898\u8bf7\u6559\uff08mysql\uff09 \u8cc7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149380"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149380"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149380\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149380"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}