{"id":1514,"date":"2022-11-07T22:45:21","date_gmt":"2022-11-07T14:45:21","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1514"},"modified":"2022-11-27T12:40:59","modified_gmt":"2022-11-27T04:40:59","slug":"%e7%ac%ac6%e7%ab%a0%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2%e8%af%be%e5%90%8e%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1514","title":{"rendered":"\u7b2c6\u7ae0\u591a\u8868\u67e5\u8be2\u8bfe\u540e\u7ec3\u4e60"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># \u7b2c06\u7ae0_\u591a\u8868\u67e5\u8be2\u8bfe\u540e\u7ec3\u4e60\r\n\r\n# 1.\u663e\u793a\u6240\u6709\u5458\u5de5\u7684\u59d3\u540d\uff0c\u90e8\u95e8\u53f7\u548c\u90e8\u95e8\u540d\u79f0\u3002\r\nSELECT e.last_name,e.department_id,d.department_name\r\nFROM employees e LEFT JOIN departments d\r\nON e.department_id = d.department_id;\r\n\r\n# 2.\u67e5\u8be290\u53f7\u90e8\u95e8\u5458\u5de5\u7684job_id\u548c90\u53f7\u90e8\u95e8\u7684location_id\r\nSELECT e.job_id,d.location_id\r\nFROM employees e JOIN departments d\r\nON e.department_id = d.department_id\r\nWHERE d.department_id = 90;\r\n\r\nDESC departments;\r\n\r\n# 3.\u9009\u62e9\u6240\u6709\u6709\u5956\u91d1\u7684\u5458\u5de5\u7684 last_name , department_name , location_id , city\r\nSELECT e.last_name,e.commission_pct,d.department_name,d.location_id,l.city\r\nFROM employees e LEFT JOIN departments d\r\nON e.department_id = d.department_id\r\nLEFT JOIN locations l\r\nON d.location_id = l.location_id\r\nWHERE e.commission_pct IS NOT NULL;     #\u4e5f\u5e94\u8be5\u662f35\u6761\u8bb0\u5f55\r\n\r\nSELECT *\r\nFROM employees\r\nWHERE commission_pct IS NOT NULL;   #35\u6761\u8bb0\u5f55\r\n\r\n# 4.\u9009\u62e9city\u5728Toronto\u5de5\u4f5c\u7684\u5458\u5de5\u7684 last_name , job_id , department_id , department_name\r\nSELECT e.last_name,e.job_id,e.department_id,d.department_name\r\nFROM employees e JOIN departments d\r\nON e.department_id = d.department_id\r\nJOIN locations l\r\nON d.location_id = l.location_id\r\nWHERE L.city = 'Toronto';\r\n\r\n#SQL92\u8bed\u6cd5:\r\nSELECT e.last_name,e.job_id,e.department_id,d.department_name\r\nFROM employees e,departments d,locations l\r\nWHERE e.department_id = d.department_id\r\nAND d.location_id = l.location_id\r\nAND l.city = 'Toronto';\r\n\r\n# 5.\u67e5\u8be2\u5458\u5de5\u6240\u5728\u7684\u90e8\u95e8\u540d\u79f0\u3001\u90e8\u95e8\u5730\u5740\u3001\u59d3\u540d\u3001\u5de5\u4f5c\u3001\u5de5\u8d44\uff0c\u5176\u4e2d\u5458\u5de5\u6240\u5728\u90e8\u95e8\u7684\u90e8\u95e8\u540d\u79f0\u4e3a\u2019Executive\u2019\r\nSELECT d.department_name,l.street_address,e.last_name,e.job_id,e.salary\r\nFROM departments d LEFT JOIN employees e\r\nON e.department_id = d.department_id\r\nLEFT JOIN locations l\r\nON d.location_id = l.location_id\r\nWHERE department_name = 'Executive';\r\n\r\nDESC departments;\r\nDESC locations;\r\n\r\n# 6.\u9009\u62e9\u6307\u5b9a\u5458\u5de5\u7684\u59d3\u540d\uff0c\u5458\u5de5\u53f7\uff0c\u4ee5\u53ca\u4ed6\u7684\u7ba1\u7406\u8005\u7684\u59d3\u540d\u548c\u5458\u5de5\u53f7\uff0c\u7ed3\u679c\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u7684\u683c\u5f0f\r\n# employees Emp# manager Mgr#\r\n# kochhar 101 king 100\r\nSELECT emp.last_name \"employees\",emp.employee_id \"Emp#\",mgr.last_name \"manager\",mgr.employee_id \"Mgr#\"\r\nFROM employees emp LEFT JOIN employees mgr\r\nON emp.manager_id = mgr.employee_id;\r\n\r\n# 7.\u67e5\u8be2\u54ea\u4e9b\u90e8\u95e8\u6ca1\u6709\u5458\u5de5\r\nSELECT d.department_id\r\nFROM employees e RIGHT JOIN departments d\r\nON e.department_id = d.department_id\r\nWHERE e.department_id IS NULL;\r\n\r\n#\u672c\u9898\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b50\u67e5\u8be2\uff1a\u6682\u65f6\u4e0d\u8bb2\r\n\r\n# 8. \u67e5\u8be2\u54ea\u4e2a\u57ce\u5e02\u6ca1\u6709\u90e8\u95e8\r\nSELECT l.location_id,l.city\r\nFROM departments d RIGHT JOIN locations l\r\nON d.location_id = l.location_id\r\nWHERE d.location_id IS NULL;\r\n\r\nSELECT department_id\r\nFROM departments\r\nWHERE department_id IN (1000,1100,1200,1300,1600);\r\n\r\n# 9. \u67e5\u8be2\u90e8\u95e8\u540d\u4e3a Sales \u6216 IT \u7684\u5458\u5de5\u4fe1\u606f\r\nSELECT e.employee_id,e.last_name,e.department_id\r\nFROM employees e JOIN departments d\r\nON e.department_id = d.department_id\r\nWHERE d.department_name IN ('Sales','IT');\r\n\r\n\r\n\r\n#1.\u6240\u6709\u6709\u95e8\u6d3e\u7684\u4eba\u5458\u4fe1\u606f\r\n# \uff08 A\u3001B\u4e24\u8868\u5171\u6709\uff09\r\n\r\n#2.\u5217\u51fa\u6240\u6709\u7528\u6237\uff0c\u5e76\u663e\u793a\u5176\u673a\u6784\u4fe1\u606f\r\n# \uff08A\u7684\u5168\u96c6\uff09\r\n\r\n#3.\u5217\u51fa\u6240\u6709\u95e8\u6d3e\r\n# \uff08B\u7684\u5168\u96c6\uff09\r\n\r\n#4.\u6240\u6709\u4e0d\u5165\u95e8\u6d3e\u7684\u4eba\u5458\r\n# \uff08A\u7684\u72ec\u6709\uff09\r\n\r\n#5.\u6240\u6709\u6ca1\u4eba\u5165\u7684\u95e8\u6d3e\r\n# \uff08B\u7684\u72ec\u6709\uff09\r\n\r\n#6.\u5217\u51fa\u6240\u6709\u4eba\u5458\u548c\u673a\u6784\u7684\u5bf9\u7167\u5173\u7cfb\r\n#  (AB\u5168\u6709)\r\n#MySQL Full Join\u7684\u5b9e\u73b0 \u56e0\u4e3aMySQL\u4e0d\u652f\u6301FULL JOIN,\u4e0b\u9762\u662f\u66ff\u4ee3\u65b9\u6cd5\r\n#left join + union(\u53ef\u53bb\u9664\u91cd\u590d\u6570\u636e)+ right join\r\n\r\n#7.\u5217\u51fa\u6240\u6709\u6ca1\u5165\u6d3e\u7684\u4eba\u5458\u548c\u6ca1\u4eba\u5165\u7684\u95e8\u6d3e\r\n# \uff08A\u7684\u72ec\u6709+B\u7684\u72ec\u6709\uff09\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[47],"class_list":["post-1514","post","type-post","status-publish","format-standard","hentry","category-mysql-code","tag-mysql"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1514"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1514"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1514\/revisions"}],"predecessor-version":[{"id":1515,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1514\/revisions\/1515"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}