{"id":480,"date":"2023-04-11T14:36:57","date_gmt":"2023-04-11T06:36:57","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=480"},"modified":"2023-04-11T14:36:57","modified_gmt":"2023-04-11T06:36:57","slug":"mysql%e5%a4%84%e7%90%86%e6%97%b6%e9%97%b4%e5%8f%8a%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2023\/04\/11\/mysql%e5%a4%84%e7%90%86%e6%97%b6%e9%97%b4%e5%8f%8a%e5%ad%97%e7%ac%a6%e4%b8%b2\/","title":{"rendered":"Mysql\u5904\u7406\u65f6\u95f4\u53ca\u5b57\u7b26\u4e32"},"content":{"rendered":"\n<p>1\u3001mysql\u67e5\u8be2\u6761\u4ef6\u81ea\u52a8\u53bb\u9664\u5de6\u8fb9\u76840<\/p>\n\n\n\n<p> \u5904\u7406\u529e\u6cd5\uff1a\u5b57\u7b26\u4e32+0,\u53ef\u4ee5\u81ea\u52a8\u8f6c\u6362 <\/p>\n\n\n\n<p>2\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee<\/p>\n\n\n\n<p>\n\nSELECT DATEDIFF(&#8216;2008-8-21,&#8217;2009-3-21&#8217;); \u8ddfSqlServer \u5dee\u4e0d\u591a\n\n<\/p>\n\n\n\n<p>3\u3001 \u5728Mysql \u6570\u636e\u5e93\u4e2d\u5b58\u5728<strong>\u4e24\u79cd\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c<\/strong>.\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b \uff1a<\/p>\n\n\n\n<p><strong>CONCAT(string1,string2,\u2026)   <\/strong>\u8bf4\u660e : string1,string2\u4ee3\u8868\u5b57\u7b26\u4e32,concat\u51fd\u6570\u5728\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u53ea\u8981\u5176\u4e2d\u4e00\u4e2a\u662fNULL,\u90a3\u4e48\u5c06\u8fd4\u56deNULL \u4f8b1: \u4f8b2:<\/p>\n\n\n\n<p><strong>CONCAT_WS(separator,str1,str2,\u2026) <\/strong>\u8bf4\u660e : string1,string2\u4ee3\u8868\u5b57\u7b26\u4e32,concat_ws \u4ee3\u8868 concat with separator,\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5176\u5b83\u53c2\u6570\u7684\u5206\u9694\u7b26\u3002\u5206\u9694\u7b26\u7684\u4f4d\u7f6e\u653e\u5728\u8981\u8fde\u63a5\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u3002\u5206\u9694\u7b26\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u5b83\u53c2\u6570\u3002\u5982\u679c\u5206\u9694\u7b26\u4e3a NULL\uff0c\u5219\u7ed3\u679c\u4e3a NULL\u3002\u51fd\u6570\u4f1a\u5ffd\u7565\u4efb\u4f55\u5206\u9694\u7b26\u53c2\u6570\u540e\u7684 NULL \u503c\u3002 \u4e3e\u4f8b1: mysql> select concat_ws(&#8216;#&#8217;,&#8217;courseName=&#8217;,&#8217;NX&#8217;,null) AS nx_courseName;<\/p>\n\n\n\n<p>MySQL\u4e2dgroup_concat\u51fd\u6570<br> \u5b8c\u6574\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<br> <strong>group_concat([DISTINCT] \u8981\u8fde\u63a5\u7684\u5b57\u6bb5 [Order BY ASC\/DESC \u6392\u5e8f\u5b57\u6bb5] [Separator &#8216;\u5206\u9694\u7b26&#8217;])<\/strong><\/p>\n\n\n\n<p>\u57fa\u672c\u67e5\u8be2<\/p>\n\n\n\n<p>mysql&gt; select * from stu1;<br>\n+&#8212;&#8212;+&#8212;&#8212;+<br>\n| id| name |<br>\n+&#8212;&#8212;+&#8212;&#8212;+<br>\n|1 | 10|<br>\n|1 | 20|<br>\n|1 | 20|<br>\n|2 | 20|<br>\n|3 | 200   |<br>\n|3 | 500   |<br>\n+&#8212;&#8212;+&#8212;&#8212;+<br>\n6 rows in set (0.00 sec)<\/p>\n\n\n\n<p>\u4ee5id\u5206\u7ec4\uff0c\u628aname\u5b57\u6bb5\u7684\u503c\u6253\u5370\u5728\u4e00\u884c\uff0c\u9017\u53f7\u5206\u9694(\u9ed8\u8ba4)<\/p>\n\n\n\n<p>mysql&gt; select id,group_concat(name) from aa group by id;<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br>\n| id| group_concat(name) |<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br>\n|1 | 10,20,20|<br>\n|2 | 20 |<br>\n|3 | 200,500|<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br>\n3 rows in set (0.00 sec)<\/p>\n\n\n\n<p>\u4ee5id\u5206\u7ec4\uff0c\u628aname\u5b57\u6bb5\u7684\u503c\u6253\u5370\u5728\u4e00\u884c\uff0c\u5206\u53f7\u5206\u9694<\/p>\n\n\n\n<p>mysql&gt; select id,group_concat(name separator &#8216;;&#8217;) from aa group by id;<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br>\n| id| group_concat(name separator &#8216;;&#8217;) |<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br>\n|1 | 10;20;20 |<br>\n|2 | 20|<br>\n|3 | 200;500   |<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br>\n3 rows in set (0.00 sec)<\/p>\n\n\n\n<p>\u4ee5id\u5206\u7ec4\uff0c\u628a\u53bb\u5197\u4f59\u7684name\u5b57\u6bb5\u7684\u503c\u6253\u5370\u5728\u4e00\u884c\uff0c<\/p>\n\n\n\n<p>\u9017\u53f7\u5206\u9694<\/p>\n\n\n\n<p>mysql&gt; select id,group_concat(distinct name) from aa group by id;<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br>\n| id| group_concat(distinct name) |<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br>\n|1 | 10,20|<br>\n|2 | 20   |<br>\n|3 | 200,500 |<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br>\n3 rows in set (0.00 sec)<\/p>\n\n\n\n<p>\u4ee5id\u5206\u7ec4\uff0c\u628aname\u5b57\u6bb5\u7684\u503c\u6253\u5370\u5728\u4e00\u884c\uff0c\u9017\u53f7\u5206\u9694\uff0c\u4ee5name\u6392\u5012\u5e8f<\/p>\n\n\n\n<p>mysql&gt; select id,group_concat(name order by name desc) from aa group by id;<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br>\n| id| group_concat(name order by name desc) |<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br>\n|1 | 20,20,10   |<br>\n|2 | 20|<br>\n|3 | 500,200|<br>\n+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br>\n3 rows in set (0.00 sec)<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8fde\u63a5\u65b9\u5f0f\u4e3a: ||<\/p>\n\n\n\n<p>mysql&gt; select *  from student;<br>\n+&#8212;-+&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+<br>\n| id | age  | score | name     | birth      |<br>\n+&#8212;-+&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+<br>\n|  1 |   23 |    78 | \u674e\u56db     | 2017-10-10 |<br>\n|  2 |   24 |    78 | zhangsan | 2017-10-10 |<br>\n|  3 |   25 |    99 | \u738b\u4e94     | 2016-05-17 |<br>\n+&#8212;-+&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;+<br>\n3 rows in set (0.00 sec)<\/p>\n\n\n\n<p>mysql> select id+999,name,name+99,name+&#8217;999&#8242; from student;<br> +&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br> | id+999 | name     | name+99 | name+&#8217;999&#8242; |<br> +&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br> |   1000 | \u674e\u56db     |      99 |        999 |<br> |   1001 | zhangsan |      99 |        999 |<br> |   1002 | \u738b\u4e94     |      99 |        999 |<br> +&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+<br> 3 rows in set, 6 warnings (0.00 sec)<br><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001mysql\u67e5\u8be2\u6761\u4ef6\u81ea\u52a8\u53bb\u9664\u5de6\u8fb9\u76840 \u5904\u7406\u529e\u6cd5\uff1a\u5b57\u7b26\u4e32+0,\u53ef\u4ee5\u81ea\u52a8\u8f6c\u6362 2\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee SELECT DATEDIFF(&#8216;2008-8-21,&#8217;2009-3-21&#8217;); \u8ddfSqlServer \u5dee\u4e0d\u591a 3\u3001 \u5728Mysql \u6570\u636e\u5e93\u4e2d\u5b58\u5728\u4e24\u79cd\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c.\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b \uff1a CONCAT(string1,string2,\u2026) \u8bf4\u660e : string1,string2\u4ee3\u8868\u5b57\u7b26\u4e32,concat\u51fd\u6570\u5728\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u53ea\u8981\u5176\u4e2d\u4e00\u4e2a\u662fNULL,\u90a3\u4e48\u5c06\u8fd4\u56deNULL \u4f8b1: \u4f8b2: CONCAT_WS(separator,str1,str2,\u2026) \u8bf4\u660e : string1,string2\u4ee3\u8868\u5b57\u7b26\u4e32,concat_ws \u4ee3\u8868 concat with separator,\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5176\u5b83\u53c2\u6570\u7684\u5206\u9694\u7b26\u3002\u5206\u9694\u7b26\u7684\u4f4d\u7f6e\u653e\u5728\u8981\u8fde\u63a5\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u3002\u5206\u9694\u7b26\u53ef\u4ee5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u5b83\u53c2\u6570\u3002\u5982\u679c\u5206\u9694\u7b26\u4e3a NULL\uff0c\u5219\u7ed3\u679c\u4e3a NULL\u3002\u51fd\u6570\u4f1a\u5ffd\u7565\u4efb\u4f55\u5206\u9694\u7b26\u53c2\u6570\u540e\u7684 NULL \u503c\u3002 \u4e3e\u4f8b1: mysql> select concat_ws(&#8216;#&#8217;,&#8217;courseName=&#8217;,&#8217;NX&#8217;,null) AS nx_courseName; MySQL\u4e2dgroup_concat\u51fd\u6570 \u5b8c\u6574\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a group_concat([DISTINCT] \u8981\u8fde\u63a5\u7684\u5b57\u6bb5 [Order BY ASC\/DESC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-480","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=480"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/480\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}