{"id":8095,"date":"2024-04-09T19:27:51","date_gmt":"2024-04-09T11:27:51","guid":{"rendered":"https:\/\/white-night.club\/?p=8095"},"modified":"2024-04-09T19:43:25","modified_gmt":"2024-04-09T11:43:25","slug":"ad178","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2024\/04\/09\/ad178\/","title":{"rendered":"580. \u7edf\u8ba1\u5404\u4e13\u4e1a\u5b66\u751f\u4eba\u6570"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/count-student-number-in-departments\/description\/\">https:\/\/leetcode.cn\/problems\/count-student-number-in-departments\/description\/<\/a><\/p>\n\n\n\n<p>\u6211\u4e25\u91cd\u6000\u7591\u662f\u56e0\u4e3a\u6211\u7f51\u901f\u592a\u6162\uff0c\u4ece\u800c\u5bfc\u81f4\u6267\u884c\u7528\u65f6\u8fc7\u957f\u3002<\/p>\n\n\n\n<p>\u8a00\u5f52\u6b63\u4f20\uff0c\u8c01JOIN\u8c01\uff0c\u8fd9\u662f\u4e2a\u95ee\u9898\u3002\u8981\u6c42\u51fa\u73b0\u6b21\u6570\uff0c\u90a3\u5c31\u5f97\u7528COUNT\u3002\u4f46\u5982\u679c\u67d0\u4e2a\u4e13\u4e1a\u6ca1\u6709\u5b66\u751f\uff0c\u8fd8\u9700\u8981\u5728\u7ed3\u679c\u96c6\u4e2d\u663e\u793a\u51fa\u6765\u4e00\u4e2a0\u3002<\/p>\n\n\n\n<p>COUNT\u600e\u4e48\u624d\u80fd\u7b97\u51fa0\uff1f\u5982\u679c\u6709\u8bb0\u5f55\uff0c\u90a3COUNT>=1\uff1b\u5982\u679c\u6709NULL\uff0cCOUNT\u8fd4\u56de0\u3002\u4e5f\u5c31\u662f\u8bf4\u53ea\u80fd\u6784\u5efa\u4e00\u4e2a\u6709NULL\u7684\u7ed3\u679c\u96c6\u53bb\u7ed9COUNT\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u600e\u4e48\u624d\u80fd\u6709NULL\uff1fLEFT JOIN\u6216\u8005RIGHT JOIN\u5457\u3002\u800c\u4e14\u5f97\u7528\u8303\u56f4\u5927\u7684\u8868\u53bbLEFT JOIN\u8303\u56f4\u5c0f\u7684\u8868\u3002\u56e0\u4e3a\u4e0d\u4e00\u5b9a\u6bcf\u4e2a\u4e13\u4e1a\u90fd\u6709\u5b66\u751f\uff0c\u4f46\u6bcf\u4e2a\u5b66\u751f\u4e00\u5b9a\u90fd\u6709\u4e13\u4e1a\u3002\u6240\u4ee5\u7528\u8303\u56f4\u5c0f\u7684\u8868\uff08\u8fd9\u91cc\u662f\u201c\u4e13\u4e1a\u8868\u201dDept\uff09\u6765\u4f5c\u4e3a\u201c\u4e3b\u8868\u201d\uff0c\u5982\u679c\u67d0\u4e2a\u4e13\u4e1a\u6ca1\u6709\u5b66\u751f\uff0c\u90a3\u4e48\u8be5\u4e13\u4e1aJOIN\u4e4b\u540e\u518d\u5206\u7ec4\uff0c\u7b2c\u4e00\u6761\u8bb0\u5f55\u5c31\u662fNULL\u3002\u8fd9\u6837\u624d\u80fd\u6574\u51fa\u4e2a\u6709NULL\u7684\u7ed3\u679c\u96c6\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">SQL<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Write your MySQL query statement below\nSELECT A.dept_name,COUNT(B.dept_id) AS student_number\nFROM Department AS A \nLEFT JOIN Student AS B \nON A.dept_id = B.dept_id\nGROUP BY A.dept_id\nORDER BY student_number DESC,dept_name ASC\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\"># Write your MySQL query <\/span><span style=\"color: #569CD6\">statement<\/span><span style=\"color: #D4D4D4\"> below<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">SELECT<\/span><span style=\"color: #D4D4D4\"> A.dept_name,<\/span><span style=\"color: #DCDCAA\">COUNT<\/span><span style=\"color: #D4D4D4\">(B.dept_id) <\/span><span style=\"color: #569CD6\">AS<\/span><span style=\"color: #D4D4D4\"> student_number<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">FROM<\/span><span style=\"color: #D4D4D4\"> Department <\/span><span style=\"color: #569CD6\">AS<\/span><span style=\"color: #D4D4D4\"> A <\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">LEFT JOIN<\/span><span style=\"color: #D4D4D4\"> Student <\/span><span style=\"color: #569CD6\">AS<\/span><span style=\"color: #D4D4D4\"> B <\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">ON<\/span><span style=\"color: #D4D4D4\"> A.dept_id = B.dept_id<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">GROUP BY<\/span><span style=\"color: #D4D4D4\"> A.dept_id<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">ORDER BY<\/span><span style=\"color: #D4D4D4\"> student_number <\/span><span style=\"color: #569CD6\">DESC<\/span><span style=\"color: #D4D4D4\">,dept_name <\/span><span style=\"color: #569CD6\">ASC<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>LEFT JOIN<\/p>\n","protected":false},"author":1,"featured_media":7932,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/8095"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=8095"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/8095\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/7932"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=8095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=8095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=8095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}