{"id":557,"date":"2010-10-01T03:46:32","date_gmt":"2010-10-01T03:46:32","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=557"},"modified":"2010-10-01T03:46:32","modified_gmt":"2010-10-01T03:46:32","slug":"sql%e8%af%ad%e5%8f%a5%e5%86%99%e6%b3%95%e5%8f%8aleft-join-right-join%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/10\/01\/sql%e8%af%ad%e5%8f%a5%e5%86%99%e6%b3%95%e5%8f%8aleft-join-right-join%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9\/","title":{"rendered":"Sql\u8bed\u53e5\u5199\u6cd5\u53caleft join, right join\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"<pre lang=\"tsql\">\r\n\u6ce8\u7528 join\u540e\uff0c \u6761\u4ef6\u8981\u5199\u5728\u4e4b\u540e\r\nselect * from bdCustomerAllocate a\r\nleft join bdFollowRecord b\r\non ( a.id = b.allocateid and b.id = (select top 1 id  from bdFollowRecord where allocateid = a.id order by followdate desc ))\r\nwhere a.SalesStaff = 'liuhong'\r\n\u800c\u4e0d\u8981\u7528\uff1a\r\nselect * from bdCustomerAllocate a\r\nleft join bdFollowRecord b\r\non a.id = b.allocateid \r\nwhere a.SalesStaff = 'liuhong'\r\nand b.id = (select top 1 id  from bdFollowRecord where allocateid = a.id order by followdate desc )\r\n\u4fdd\u8bc1where \u53e5\u4e2d\u7684\u6761\u4ef6\u90fd\u662ffrom \u540e\u7684\u8868\uff0c\u800c\u4e0d\u662fjoin\u7684\u8868\r\n\r\n \r\n1\uff0c\u6761\u4ef6\u5199\u5728left join, right join\u96c6\u5408\u5185\u3002\r\n\uff08\u4f5cleft join\u5f97\u5230\u7684join \u96c6\u5408\u4e00\u822c\u662f\u7edf\u8ba1\u6570\u636e\uff0c\u6545\u4e0e\u7edf\u8ba1\u76f8\u5173\u7684\u6761\u4ef6\u90fd\u8981\u653e\u5728left join\u4e4b\u5185\uff09\r\n\r\n(1)  SELECT DISTINCT c.account FROM frmUser c\r\n(2)  LEFT JOIN bdCustomerAllocate a\r\n(3)  ON c.Account = a.SalesStaff\r\n(4)  WHERE a.SalesStaff in ( SELECT ControlAccount FROM permUserRelation WHERE CurrentAccount = 'luni' )\r\n\r\n\u82e5\u6267\u884c\u524d\u4e09\u6b65\uff0c\u5219\u7ed3\u679c\u662f\u4ee5frmUser\u4e3a\u4e3b\u7684\u6570\u636e\u96c6\uff0c\u82e5\u5168\u90e8\u6267\u884c\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u597d\u8c61\u4ee5bdCustomerAllocate\u4e3a\u4e3b\uff0c\u6ca1\u6709\u8fbe\u5230\u5de6\u8fde\u63a5\u7684\u6548\u679c\u3002\u539f\u56e0\uff1a\u5206\u6e05where\u7684\u4f5c\u7528\uff0c\u524d\u4e09\u6b65\u5f97\u5230\u7684\u6570\u636e\u96c6\uff0c\u5728where\u4e2d\u4f1a\u5c06\u4e0d\u6ee1\u8db3\u6761\u4ef6\u7684\u9664\u53bb\uff0c\u56e0\u662f\u4ee5\u5de6\u8fde\u63a5bdCustomerAllocater\u7684SalesStaff\u4f5c\u5224\u65ad\uff0c\u6545\u7ed3\u679c\u96c6\u5c06\u4ee5bdCustomerAllocater\u4e3a\u4e3b\uff0c\u6ca1\u6709\u8fbe\u5230\u5de6\u8fde\u63a5\u6548\u679c\u3002\r\n\r\n\u6545\u5728\u5199 left join \u65f6\uff0cjoin \u7684\u6570\u636e\u96c6\u6700\u597d\u6267\u884c\u53bb\u6389\u5168\u90e8\u6761\u4ef6\uff0c\u800c\u4e0d\u662f\u5728left join\u4e4b\u540e\u518d\u7528where\u53bb\u9664\u3002\r\nSELECT DISTINCT c.account FROM frmUser c\r\nLEFT JOIN ( SELECT SalesStaff FROM bdCustomerAllocate WHERE SalesStaff in ( SELECT ControlAccount FROM permUserRelation WHERE CurrentAccount = 'luni' ) ) a\r\nON c.Account = a.SalesStaff\r\n\r\n2\uff0c\u5b50\u96c6\u5408\u540e\u8981\u6709\u53e6\u540d\uff1a\r\nSELECT * FROM ( SELECT * FROM bdCustomerAllocate ) a  \/\/\u65e0\u522b\u540da\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u4e0d\u6307\u5b9a\u522b\u540d\uff0c\u600e\u4e48\u5728\u6761\u4ef6\u4e2d\u5f15\u7528\u6570\u636e\u96c6\u4e2d\u7684\u5b57\u6bb5\u3002\r\n\r\n3\uff0c from \u540e\u7684 join\u662f\u5bf9\u8868\u96c6\u5408\u7684\u4e00\u79cd\u8865\u5145\r\n\r\nSELECT DISTINCT c.account,d.* FROM frmUser c\r\nJOIN bdCustomerAllocate a\r\nON c.Account = a.SalesStaff\r\n, bdCustomerAllocate d\r\n\r\njoin\u76f8\u5f53\u4e8e\u5bf9from\u540e\u7684\u8868\u5bf9\u4e00\u79cd\u8865\u5145\uff0c\u4e4b\u540e\u518d\u7528\u9017\u53f7\u5f15\u7528\u5176\u5b83\u8868\u3002\u4e0b\u9762\u7684\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3ajoin\u4f5c\u7528\u5728\u7d27\u8ddf\u5b83\u7684\u8868\u540e\u9762\uff0c\u6b64\u65f6\u4f1a\u4f5c\u7528\u5728d\u4e0a\uff0cd\u6ca1\u6709account\r\nSELECT DISTINCT c.account,d.* FROM frmUser c, bdCustomerAllocate d\r\nJOIN bdCustomerAllocate a\r\nON c.Account = a.SalesStaff\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\u7528 join\u540e\uff0c \u6761\u4ef6\u8981\u5199\u5728\u4e4b\u540e select * from bdCustomerAllocate a le [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-557","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=557"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/557\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}