{"id":182516,"date":"2024-11-09T23:06:17","date_gmt":"2024-11-09T15:06:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/182516\/"},"modified":"2024-11-09T23:06:18","modified_gmt":"2024-11-09T15:06:18","slug":"%e5%8d%81%e5%80%8b%e9%9d%9e%e5%b8%b8%e5%af%a6%e7%94%a8%e7%9a%84mysql%e5%87%bd%e6%95%b8%ef%bc%8c%e4%bd%a0%e8%a6%81%e4%b8%8d%e8%a6%81%e9%80%b2%e4%be%86%e9%a0%98%e8%b5%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/182516\/","title":{"rendered":"\u5341\u500b\u975e\u5e38\u5be6\u7528\u7684MySQL\u51fd\u6578\uff0c\u4f60\u8981\u4e0d\u8981\u9032\u4f86\u9818\u8d70"},"content":{"rendered":"<h1 id=\"%e5%8d%81%e5%80%8b%e9%9d%9e%e5%b8%b8%e5%af%a6%e7%94%a8%e7%9a%84mysql%e5%87%bd%e6%95%b8%ef%bc%8c%e4%bd%a0%e8%a6%81%e4%b8%8d%e8%a6%81%e9%80%b2%e4%be%86%e9%a0%98%e8%b5%b0-eeMVGwoCdX\">\u5341\u500b\u975e\u5e38\u5be6\u7528\u7684MySQL\u51fd\u6578\uff0c\u4f60\u8981\u4e0d\u8981\u9032\u4f86\u9818\u8d70<\/h1>\n<p>MySQL\u662f\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u56e0\u5176\u9ad8\u6548\u80fd\u548c\u9748\u6d3b\u6027\u800c\u53d7\u5230\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u5728\u65e5\u5e38\u7684\u6578\u64da\u8655\u7406\u4e2d\uff0c\u638c\u63e1\u4e00\u4e9b\u5be6\u7528\u7684MySQL\u51fd\u6578\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5341\u500b\u975e\u5e38\u5be6\u7528\u7684MySQL\u51fd\u6578\uff0c\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002<\/p>\n<h2 id=\"1-concat-eeMVGwoCdX\">1. CONCAT()<\/h2>\n<p>CONCAT()\u51fd\u6578\u7528\u65bc\u5c07\u591a\u500b\u5b57\u7b26\u4e32\u9023\u63a5\u6210\u4e00\u500b\u5b57\u7b26\u4e32\u3002\u9019\u5728\u9700\u8981\u5408\u4f75\u591a\u500b\u5b57\u6bb5\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;<\/code><\/pre>\n<h2 id=\"2-count-eeMVGwoCdX\">2. COUNT()<\/h2>\n<p>COUNT()\u51fd\u6578\u7528\u65bc\u8a08\u7b97\u67e5\u8a62\u7d50\u679c\u4e2d\u7684\u884c\u6578\u3002\u9019\u5c0d\u65bc\u7d71\u8a08\u6578\u64da\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<pre><code>SELECT COUNT(*) FROM orders WHERE status = 'completed';<\/code><\/pre>\n<h2 id=\"3-avg-eeMVGwoCdX\">3. AVG()<\/h2>\n<p>AVG()\u51fd\u6578\u7528\u65bc\u8a08\u7b97\u67d0\u4e00\u5217\u7684\u5e73\u5747\u503c\u3002\u9019\u5728\u5206\u6790\u6578\u64da\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT AVG(price) AS average_price FROM products;<\/code><\/pre>\n<h2 id=\"4-sum-eeMVGwoCdX\">4. SUM()<\/h2>\n<p>SUM()\u51fd\u6578\u7528\u65bc\u8a08\u7b97\u67d0\u4e00\u5217\u7684\u7e3d\u548c\u3002\u9019\u5c0d\u65bc\u8ca1\u52d9\u5831\u8868\u6216\u92b7\u552e\u6578\u64da\u7684\u5206\u6790\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<pre><code>SELECT SUM(amount) AS total_sales FROM transactions;<\/code><\/pre>\n<h2 id=\"5-max-%e5%92%8c-min-eeMVGwoCdX\">5. MAX() \u548c MIN()<\/h2>\n<p>MAX()\u548cMIN()\u51fd\u6578\u5206\u5225\u7528\u65bc\u67e5\u627e\u67d0\u4e00\u5217\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u9019\u5728\u6578\u64da\u7bc4\u570d\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT MAX(salary) AS highest_salary, MIN(salary) AS lowest_salary FROM employees;<\/code><\/pre>\n<h2 id=\"6-ifnull-eeMVGwoCdX\">6. IFNULL()<\/h2>\n<p>IFNULL()\u51fd\u6578\u7528\u65bc\u6aa2\u67e5\u4e00\u500b\u503c\u662f\u5426\u70baNULL\uff0c\u5982\u679c\u662f\uff0c\u5247\u8fd4\u56de\u6307\u5b9a\u7684\u66ff\u4ee3\u503c\u3002\u9019\u5728\u8655\u7406\u7f3a\u5931\u6578\u64da\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT IFNULL(discount, 0) AS discount_amount FROM orders;<\/code><\/pre>\n<h2 id=\"7-date_format-eeMVGwoCdX\">7. DATE_FORMAT()<\/h2>\n<p>DATE_FORMAT()\u51fd\u6578\u7528\u65bc\u683c\u5f0f\u5316\u65e5\u671f\u3002\u9019\u5728\u9700\u8981\u4ee5\u7279\u5b9a\u683c\u5f0f\u986f\u793a\u65e5\u671f\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT DATE_FORMAT(order_date, '%Y-%m-%d') AS formatted_date FROM orders;<\/code><\/pre>\n<h2 id=\"8-group_concat-eeMVGwoCdX\">8. GROUP_CONCAT()<\/h2>\n<p>GROUP_CONCAT()\u51fd\u6578\u7528\u65bc\u5c07\u591a\u884c\u6578\u64da\u5408\u4f75\u70ba\u4e00\u884c\uff0c\u4e26\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u9694\u3002\u9019\u5728\u751f\u6210\u5831\u544a\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT GROUP_CONCAT(product_name SEPARATOR ', ') AS product_list FROM order_items GROUP BY order_id;<\/code><\/pre>\n<h2 id=\"9-coalesce-eeMVGwoCdX\">9. COALESCE()<\/h2>\n<p>COALESCE()\u51fd\u6578\u8fd4\u56de\u5176\u53c3\u6578\u4e2d\u7b2c\u4e00\u500b\u975eNULL\u7684\u503c\u3002\u9019\u5728\u8655\u7406\u591a\u500b\u53ef\u80fd\u70baNULL\u7684\u503c\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT COALESCE(phone, mobile, 'N\/A') AS contact_number FROM users;<\/code><\/pre>\n<h2 id=\"10-case-eeMVGwoCdX\">10. CASE<\/h2>\n<p>CASE\u8a9e\u53e5\u7528\u65bc\u5728\u67e5\u8a62\u4e2d\u9032\u884c\u689d\u4ef6\u908f\u8f2f\u3002\u9019\u4f7f\u5f97\u67e5\u8a62\u66f4\u52a0\u9748\u6d3b\u548c\u5f37\u5927\u3002<\/p>\n<pre><code>SELECT name, \n       CASE \n           WHEN score &gt;= 90 THEN 'A' \n           WHEN score &gt;= 80 THEN 'B' \n           ELSE 'C' \n       END AS grade \nFROM students;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-eeMVGwoCdX\">\u7e3d\u7d50<\/h2>\n<p>\u4ee5\u4e0a\u4ecb\u7d39\u7684\u5341\u500bMySQL\u51fd\u6578\u662f\u6578\u64da\u5eab\u7ba1\u7406\u548c\u64cd\u4f5c\u4e2d\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\u3002\u7121\u8ad6\u4f60\u662f\u6578\u64da\u5206\u6790\u5e2b\u9084\u662f\u958b\u767c\u8005\uff0c\u638c\u63e1\u9019\u4e9b\u51fd\u6578\u90fd\u80fd\u5e6b\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u8655\u7406\u6578\u64da\u3002\u5982\u679c\u4f60\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>\u6216<a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\u4f86\u652f\u6301\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u548c\u6578\u64da\u5b58\u5132\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5341\u500b\u975e\u5e38\u5be6\u7528\u7684MySQL\u51fd\u6578\uff0c\u63d0\u5347\u4f60\u7684\u6578\u64da\u5eab\u7ba1\u7406\u6280\u80fd\uff0c\u8b93\u4f60\u7684\u67e5\u8a62\u66f4\u9ad8\u6548\uff0c\u5feb\u4f86\u9818\u8d70\u9019\u4e9b\u6280\u5de7\uff01<\/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-182516","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/182516","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=182516"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/182516\/revisions"}],"predecessor-version":[{"id":182517,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/182516\/revisions\/182517"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=182516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=182516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=182516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}