{"id":6341,"date":"2024-11-14T09:31:05","date_gmt":"2024-11-14T01:31:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6341\/"},"modified":"2024-11-14T09:31:05","modified_gmt":"2024-11-14T01:31:05","slug":"mysql%e6%9f%a5%e8%af%a2%e8%af%ad%e5%8f%a5%e6%b1%87%e6%80%bb","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6341\/","title":{"rendered":"mysql\u67e5\u8be2\u8bed\u53e5\u6c47\u603b"},"content":{"rendered":"<blockquote><p>\n  mysql\u67e5\u8be2\u8bed\u53e5\u6c47\u603b\uff1a\u4e00\u3001\u57fa\u672c\u67e5\u8be2\u8bed\u53e5\uff1aselect\u6307\u5b9a\u5217\uff0cfrom\u6307\u5b9a\u8868\uff0cwhere\u8bbe\u7f6e\u6761\u4ef6\u3002\u4e8c\u3001\u805a\u5408\u51fd\u6570\uff1acount\u8ba1\u6570\uff0csum\u6c42\u548c\uff0cavg\u6c42\u5e73\u5747\uff0cmin\u6c42\u6700\u5c0f\u503c\uff0cmax\u6c42\u6700\u5927\u503c\u3002\u4e09\u3001\u6392\u5e8f\u548c\u9650\u5236\uff1aorder by\u6392\u5e8f\uff0climit\u9650\u5236\u884c\u6570\u3002\u56db\u3001\u5206\u7ec4\u548c\u805a\u5408\uff1agroup by\u5206\u7ec4\uff0chaving\u8fc7\u6ee4\u5206\u7ec4\u7ed3\u679c\u3002\u4e94\u3001\u8fde\u63a5\u67e5\u8be2\uff1ajoin\u8fde\u63a5\u8868\uff0cleft\/right\/inner\u6307\u5b9a\u8fde\u63a5\u65b9\u5f0f\u3002\u516d\u3001\u5b50\u67e5\u8be2\uff1ain\/not in\u6bd4\u8f83\u5b50\u67e5\u8be2\u7ed3\u679c\uff0cexists\/not exists\u68c0\u67e5\u5b50\u67e5\u8be2\u662f\u5426\u5b58\u5728\u7ed3\u679c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/01\/2024080102311852869.jpg\" class=\"aligncenter\" title=\"mysql\u67e5\u8be2\u8bed\u53e5\u6c47\u603b\u63d2\u56fe\" alt=\"mysql\u67e5\u8be2\u8bed\u53e5\u6c47\u603b\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u67e5\u8be2\u8bed\u53e5\u6c47\u603b<\/strong><\/p>\n<p><strong>\u4e00\u3001\u57fa\u672c\u67e5\u8be2\u8bed\u53e5<\/strong><\/p>\n<ul>\n<li> <strong>SELECT\uff1a<\/strong>\u9009\u62e9\u8981\u67e5\u8be2\u7684\u5217<\/li>\n<li> <strong>FROM\uff1a<\/strong>\u6307\u5b9a\u8981\u67e5\u8be2\u7684\u8868<\/li>\n<li> <strong>WHERE\uff1a<\/strong>\u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong>\u9009\u62e9 id \u548c name \u5217\uff0c\u4ece users \u8868\u4e2d\u67e5\u8be2\u6240\u6709\u7528\u6237<\/p>\n<pre>SELECT id, name FROM users;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u3001<\/strong><\/p>\n<ul>\n<li> <strong>COUNT\uff1a<\/strong>\u8ba1\u7b97\u884c\u6570<\/li>\n<li> <strong>SUM\uff1a<\/strong>\u8ba1\u7b97\u603b\u548c<\/li>\n<li> <strong>AVG\uff1a<\/strong>\u8ba1\u7b97\u5e73\u5747\u503c<\/li>\n<li> <strong>MIN\uff1a<\/strong>\u8ba1\u7b97\u6700\u5c0f\u503c<\/li>\n<li> <strong>MAX\uff1a<\/strong>\u8ba1\u7b97\u6700\u5927\u503c<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong>\u8ba1\u7b97 users \u8868\u4e2d\u7528\u6237\u7684\u603b\u6570\u91cf<\/p>\n<pre>SELECT COUNT(*) FROM users;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e09\u3001\u6392\u5e8f<\/strong><\/p>\n<ul>\n<li> <strong>ORDER BY\uff1a<\/strong>\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4f7f\u7528 ASC\uff08\u5347\u5e8f\uff09\u6216 DESC\uff08\u964d\u5e8f\uff09<\/li>\n<li> <strong>LIMIT\uff1a<\/strong>\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong>\u6839\u636e id \u5347\u5e8f\u6392\u5e8f\u5e76\u8fd4\u56de\u524d 5 \u4e2a\u7528\u6237<\/p>\n<pre>SELECT id, name FROM users ORDER BY id ASC LIMIT 5;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u56db\u3001\u5206\u7ec4\u548c\u805a\u5408<\/strong><\/p>\n<ul>\n<li> <strong>GROUP BY\uff1a<\/strong>\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4<\/li>\n<li> <strong>HAVING\uff1a<\/strong>\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong>\u6309\u6027\u522b\u5bf9\u7528\u6237\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u7ec4\u7684\u7528\u6237\u6570<\/p>\n<pre>SELECT gender, COUNT(*) AS user_count FROM users GROUP BY gender;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e94\u3001\u8fde\u63a5\u67e5\u8be2<\/strong><\/p>\n<ul>\n<li> <strong>JOIN\uff1a<\/strong>\u5c06\u591a\u4e2a\u8868\u8fde\u63a5\u8d77\u6765\uff0c\u57fa\u4e8e\u516c\u5171\u5217<\/li>\n<li> <strong>LEFT JOIN\uff1a<\/strong>\u5305\u62ec\u5de6\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5728\u53f3\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879<\/li>\n<li> <strong>RIGHT JOIN\uff1a<\/strong>\u5305\u62ec\u53f3\u8868\u7684\u6240\u6709\u884c\uff0c\u5373\u4f7f\u5728\u5de6\u8868\u4e2d\u6ca1\u6709\u5339\u914d\u9879<\/li>\n<li> <strong>INNER JOIN\uff1a<\/strong>\u4ec5\u8fd4\u56de\u5177\u6709\u5339\u914d\u9879\u7684\u884c<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong>\u8fde\u63a5 users \u8868\u548c orders \u8868\uff0c\u57fa\u4e8e user_id \u5217<\/p>\n<pre>SELECT * FROM users u INNER JOIN orders o ON u.user_id = o.user_id;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u516d\u3001\u5b50\u67e5\u8be2<\/strong><\/p>\n<ul>\n<li> <strong>IN\/NOT IN\uff1a<\/strong>\u5c06\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u4f5c\u4e3a\u6bd4\u8f83\u6761\u4ef6<\/li>\n<li> <strong>EXISTS\/NOT EXISTS\uff1a<\/strong>\u68c0\u67e5\u5b50\u67e5\u8be2\u662f\u5426\u5b58\u5728\u7ed3\u679c<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong>\u67e5\u627e\u5728 orders \u8868\u4e2d\u5177\u6709\u8ba2\u5355\u7684\u7528\u6237<\/p>\n<pre>SELECT * FROM users WHERE id IN (SELECT user_id FROM orders);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u67e5\u8be2\u8bed\u53e5\u6c47\u603b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql\u67e5\u8be2\u8bed\u53e5\u6c47\u603b\uff1a\u4e00\u3001\u57fa\u672c\u67e5\u8be2\u8bed\u53e5\uff1aselect\u6307\u5b9a\u5217\uff0cfrom\u6307\u5b9a\u8868\uff0cwhere\u8bbe\u7f6e\u6761\u4ef6\u3002\u4e8c\u3001\u805a\u5408\u51fd\u6570\uff1acount\u8ba1\u6570\uff0csum\u6c42\u548c\uff0cavg\u6c42\u5e73\u5747\uff0cmin\u6c42\u6700\u5c0f\u503c\uff0cmax\u6c42\u6700\u5927\u503c\u3002\u4e09\u3001\u6392\u5e8f\u548c\u9650\u5236\uff1aorder by\u6392\u5e8f\uff0climit\u9650\u5236\u884c\u6570\u3002\u56db\u3001\u5206\u7ec4\u548c\u805a\u5408\uff1agroup by\u5206\u7ec4\uff0chaving\u8fc7\u6ee4\u5206\u7ec4\u7ed3\u679c\u3002\u4e94\u3001\u8fde\u63a5\u67e5\u8be2\uff1ajoin\u8fde\u63a5\u8868\uff0cleft\/right\/inner\u6307\u5b9a\u8fde\u63a5\u65b9\u5f0f\u3002\u516d\u3001\u5b50\u67e5\u8be2\uff1ain\/not in\u6bd4\u8f83\u5b50\u67e5\u8be2\u7ed3\u679c\uff0cexists\/not exists\u68c0\u67e5\u5b50\u67e5\u8be2\u662f\u5426\u5b58\u5728\u7ed3\u679c\u3002 MySQL \u67e5\u8be2\u8bed\u53e5\u6c47\u603b \u4e00\u3001\u57fa\u672c\u67e5\u8be2\u8bed\u53e5 SELECT\uff1a\u9009\u62e9\u8981\u67e5\u8be2\u7684\u5217 FROM\uff1a\u6307\u5b9a\u8981\u67e5\u8be2\u7684\u8868 WHERE\uff1a\u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6 \u793a\u4f8b\uff1a\u9009\u62e9 id \u548c name \u5217\uff0c\u4ece users \u8868\u4e2d\u67e5\u8be2\u6240\u6709\u7528\u6237 SELECT id, name FROM users; \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001 COUNT\uff1a\u8ba1\u7b97\u884c\u6570 SUM\uff1a\u8ba1\u7b97\u603b\u548c AVG\uff1a\u8ba1\u7b97\u5e73\u5747\u503c MIN\uff1a\u8ba1\u7b97\u6700\u5c0f\u503c MAX\uff1a\u8ba1\u7b97\u6700\u5927\u503c \u793a\u4f8b\uff1a\u8ba1\u7b97 users \u8868\u4e2d\u7528\u6237\u7684\u603b\u6570\u91cf SELECT COUNT(*) FROM users; \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u6392\u5e8f ORDER BY\uff1a\u6839\u636e\u6307\u5b9a\u5217\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4f7f\u7528 ASC\uff08\u5347\u5e8f\uff09\u6216 DESC\uff08\u964d\u5e8f\uff09 LIMIT\uff1a\u9650\u5236\u8fd4\u56de\u7684\u884c\u6570 \u793a\u4f8b\uff1a\u6839\u636e id \u5347\u5e8f\u6392\u5e8f\u5e76\u8fd4\u56de\u524d 5 \u4e2a\u7528\u6237 SELECT id, name FROM users ORDER [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6341","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6341"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6341\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}