{"id":76132,"date":"2024-10-16T04:07:13","date_gmt":"2024-10-15T20:07:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76132\/"},"modified":"2024-10-16T04:07:13","modified_gmt":"2024-10-15T20:07:13","slug":"mysql%e9%82%a3%e4%ba%9b%e4%b8%8d%e5%b8%b8%e7%94%a8%e7%9a%84%e4%b8%80%e4%ba%9bsql%e8%aa%9e%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76132\/","title":{"rendered":"MySQL\u90a3\u4e9b\u4e0d\u5e38\u7528\u7684\u4e00\u4e9bSQL\u8a9e\u53e5"},"content":{"rendered":"<h1 id=\"mysql%e9%82%a3%e4%ba%9b%e4%b8%8d%e5%b8%b8%e7%94%a8%e7%9a%84%e4%b8%80%e4%ba%9bsql%e8%aa%9e%e5%8f%a5-tPbeycIkxX\">MySQL\u90a3\u4e9b\u4e0d\u5e38\u7528\u7684\u4e00\u4e9bSQL\u8a9e\u53e5<\/h1>\n<p>\u5728\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u5eab\u7ba1\u7406\u6642\uff0c\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u901a\u5e38\u6703\u4f7f\u7528\u4e00\u4e9b\u5e38\u898b\u7684SQL\u8a9e\u53e5\uff0c\u5982SELECT\u3001INSERT\u3001UPDATE\u548cDELETE\u3002\u7136\u800c\uff0cMySQL\u9084\u63d0\u4f9b\u4e86\u8a31\u591a\u4e0d\u5e38\u7528\u4f46\u540c\u6a23\u5f37\u5927\u7684SQL\u8a9e\u53e5\uff0c\u9019\u4e9b\u8a9e\u53e5\u53ef\u4ee5\u5e6b\u52a9\u7528\u6236\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684MySQL SQL\u8a9e\u53e5\u53ca\u5176\u61c9\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"1-case%e8%aa%9e%e5%8f%a5-tPbeycIkxX\">1. CASE\u8a9e\u53e5<\/h2>\n<p>CASE\u8a9e\u53e5\u662f\u4e00\u7a2e\u689d\u4ef6\u8a9e\u53e5\uff0c\u5141\u8a31\u7528\u6236\u5728\u67e5\u8a62\u4e2d\u9032\u884c\u689d\u4ef6\u5224\u65b7\u3002\u9019\u5728\u9700\u8981\u6839\u64da\u4e0d\u540c\u689d\u4ef6\u8fd4\u56de\u4e0d\u540c\u7d50\u679c\u6642\u7279\u5225\u6709\u7528\u3002<\/p>\n<pre><code>SELECT \n    name, \n    CASE \n        WHEN score &gt;= 90 THEN '\u512a\u79c0'\n        WHEN score &gt;= 75 THEN '\u826f\u597d'\n        WHEN score &gt;= 60 THEN '\u53ca\u683c'\n        ELSE '\u4e0d\u53ca\u683c'\n    END AS grade\nFROM students;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6839\u64da\u5b78\u751f\u7684\u5206\u6578\u8fd4\u56de\u76f8\u61c9\u7684\u8a55\u7d1a\u3002<\/p>\n<h2 id=\"2-coalesce%e5%87%bd%e6%95%b8-tPbeycIkxX\">2. COALESCE\u51fd\u6578<\/h2>\n<p>COALESCE\u51fd\u6578\u7528\u65bc\u8fd4\u56de\u7b2c\u4e00\u500b\u975eNULL\u7684\u503c\u3002\u9019\u5728\u8655\u7406\u7f3a\u5931\u6578\u64da\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT \n    name, \n    COALESCE(phone, '\u7121\u96fb\u8a71') AS contact_number\nFROM customers;<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u8fd4\u56de\u5ba2\u6236\u7684\u96fb\u8a71\u865f\u78bc\uff0c\u5982\u679c\u96fb\u8a71\u865f\u78bc\u70baNULL\uff0c\u5247\u986f\u793a\u201c\u7121\u96fb\u8a71\u201d\u3002<\/p>\n<h2 id=\"3-group_concat%e5%87%bd%e6%95%b8-tPbeycIkxX\">3. GROUP_CONCAT\u51fd\u6578<\/h2>\n<p>GROUP_CONCAT\u51fd\u6578\u53ef\u4ee5\u5c07\u591a\u884c\u6578\u64da\u5408\u4f75\u70ba\u4e00\u884c\uff0c\u4e26\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u9694\u3002\u9019\u5728\u9700\u8981\u5c07\u591a\u500b\u503c\u5408\u4f75\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT \n    department, \n    GROUP_CONCAT(employee_name SEPARATOR ', ') AS employees\nFROM employees\nGROUP BY department;<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u8fd4\u56de\u6bcf\u500b\u90e8\u9580\u7684\u54e1\u5de5\u540d\u7a31\uff0c\u4e26\u7528\u9017\u865f\u5206\u9694\u3002<\/p>\n<h2 id=\"4-rollup%e5%92%8ccube-tPbeycIkxX\">4. ROLLUP\u548cCUBE<\/h2>\n<p>ROLLUP\u548cCUBE\u662f\u7528\u65bc\u751f\u6210\u532f\u7e3d\u6578\u64da\u7684\u5f37\u5927\u5de5\u5177\u3002ROLLUP\u53ef\u4ee5\u751f\u6210\u5206\u7d44\u7684\u7e3d\u8a08\uff0c\u800cCUBE\u5247\u53ef\u4ee5\u751f\u6210\u6240\u6709\u53ef\u80fd\u7684\u7d44\u5408\u3002<\/p>\n<pre><code>SELECT \n    department, \n    job_title, \n    COUNT(*) AS employee_count\nFROM employees\nGROUP BY department, job_title WITH ROLLUP;<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u8fd4\u56de\u6bcf\u500b\u90e8\u9580\u548c\u8077\u4f4d\u7684\u54e1\u5de5\u6578\u91cf\uff0c\u4e26\u5728\u6700\u5f8c\u6dfb\u52a0\u7e3d\u8a08\u884c\u3002<\/p>\n<h2 id=\"5-exists%e5%92%8cnot-exists-tPbeycIkxX\">5. EXISTS\u548cNOT EXISTS<\/h2>\n<p>EXISTS\u548cNOT EXISTS\u7528\u65bc\u6aa2\u67e5\u5b50\u67e5\u8a62\u662f\u5426\u8fd4\u56de\u7d50\u679c\u3002\u9019\u5728\u9700\u8981\u6839\u64da\u5176\u4ed6\u8868\u7684\u6578\u64da\u9032\u884c\u689d\u4ef6\u7be9\u9078\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT \n    name \nFROM students s\nWHERE EXISTS (\n    SELECT 1 \n    FROM enrollments e \n    WHERE e.student_id = s.id\n);<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u8fd4\u56de\u6240\u6709\u5df2\u8a3b\u518a\u7684\u5b78\u751f\u540d\u7a31\u3002<\/p>\n<h2 id=\"6-union%e5%92%8cunion-all-tPbeycIkxX\">6. UNION\u548cUNION ALL<\/h2>\n<p>UNION\u548cUNION ALL\u7528\u65bc\u5408\u4f75\u591a\u500b\u67e5\u8a62\u7684\u7d50\u679c\u3002UNION\u6703\u81ea\u52d5\u53bb\u9664\u91cd\u8907\u7684\u884c\uff0c\u800cUNION ALL\u5247\u4fdd\u7559\u6240\u6709\u884c\u3002<\/p>\n<pre><code>SELECT name FROM students\nUNION\nSELECT name FROM teachers;<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u8fd4\u56de\u5b78\u751f\u548c\u6559\u5e2b\u7684\u540d\u7a31\uff0c\u4e26\u53bb\u9664\u91cd\u8907\u7684\u540d\u7a31\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tPbeycIkxX\">\u7e3d\u7d50<\/h2>\n<p>\u4ee5\u4e0a\u4ecb\u7d39\u7684MySQL\u4e0d\u5e38\u7528SQL\u8a9e\u53e5\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u66f4\u52a0\u9748\u6d3b\u548c\u9ad8\u6548\u3002\u638c\u63e1\u9019\u4e9b\u8a9e\u53e5\u4e0d\u50c5\u80fd\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\uff0c\u9084\u80fd\u4f7f\u6578\u64da\u8655\u7406\u66f4\u52a0\u7c21\u4fbf\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684MySQL\u74b0\u5883\uff0c\u4eab\u53d7\u66f4\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22MySQL\u4e2d\u4e0d\u5e38\u7528\u7684SQL\u8bed\u53e5\uff0c\u63d0\u5347\u6570\u636e\u5e93\u64cd\u4f5c\u6280\u5de7\uff0c\u53d1\u6398\u9690\u85cf\u529f\u80fd\uff0c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-76132","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=76132"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76132\/revisions"}],"predecessor-version":[{"id":76133,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76132\/revisions\/76133"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}