{"id":8385,"date":"2023-03-10T13:19:03","date_gmt":"2023-03-10T05:19:03","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=8385"},"modified":"2023-03-13T19:20:33","modified_gmt":"2023-03-13T11:20:33","slug":"using-variable-concat-and-execute-sql","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2023\/03\/using-variable-concat-and-execute-sql\/","title":{"rendered":"\u5728MySQL\u4e2d\u4f7f\u7528\u6570\u7ec4\u53d8\u91cf\u62fc\u63a5\u5e76\u6267\u884cSQL"},"content":{"rendered":"\n<p>\u5728\u65e5\u5e38\u7f16\u5199SQL\u4e2d\uff0c\u4f7f\u7528\u53d8\u91cf\u6765\u7075\u6d3b\u7684\u6784\u5efa\u6267\u884c\u7684SQL\u8bed\u53e5\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u3002\u5c31\u50cf\u5728\u7f16\u7a0b\u4e2d\u4f7f\u7528\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u7b49\u662f\u4e00\u6837\u7684\u9053\u7406\u3002\u672c\u6587\u4ecb\u7ecd\u4f7f\u7528JSON\u51fd\u6570\uff08<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/json-creation-functions.html#function_json-array\">JSON_ARRAY<\/a>\u3001<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/json-search-functions.html#function_json-search\">JSON_SEARCH<\/a>\uff09\u3001\u4ee5\u53ca <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/string-functions.html#function_find-in-set\">FIND_IN_SET<\/a> \u5982\u4f55\u7b80\u5355\u7684\u5b9e\u73b0\u5e26\u6709\u6570\u636e\u7ec4\u53d8\u91cf\u7684SQL\u62fc\u63a5\u4e0e\u6267\u884c\u3002\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u5219\u65e0\u9700\u4f7f\u7528CONCAT\u51fd\u6570\u62fc\u63a5SQL\uff0c\u518d\u4f7f\u7528PREPARE\/EXECUTE\u8bed\u6cd5\u6267\u884c\uff0c\u6240\u4ee5\u4f1a\u66f4\u52a0\u7b80\u6d01\uff0c\u53ef\u8bfb\u6027\u3001\u6269\u5c55\u6027\u66f4\u5f3a\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u7684\uff0c\u4f8b\u5982\u9700\u8981\u518d\u67e5\u8be2\u7ed3\u679c\u4e2d\u627e\u5230u_name\u5728\u6570\u7ec4@u_list_j\u4e2d\u7684\u8bb0\u5f55\uff0c\u90a3\u4e48\u4f7f\u7528JSON\u65b9\u5f0f\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u6a21\u5f0f\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">SET @u_list_j = JSON_ARRAY('zhou','wu','zheng','wang','zhuge');\n\nSELECT \n    id,u_name \nFROM \n    t_01 \nWHERE \n    JSON_SEARCH(@u_list_j,'one',t_01.u_name);<\/code><\/pre>\n\n\n\n<p>\u53e6\u4e00\u79cd\u8f83\u4e3a\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/string-functions.html#function_find-in-set\"> FIND_IN_SET <\/a>\u51fd\u6570\u3002\u5176\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">set @u_list_s = 'zhou,wu,zheng,wang,zhuge';\n\nSELECT \n    u_name \nFROM \n    t_01 \nWHERE \n    FIND_IN_SET(t_01.u_name,@u_list_s);<\/code><\/pre>\n\n\n\n<p>\u53e6\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528 <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/string-functions.html#function_locate\">LOCATE<\/a> \u51fd\u6570\u5b9e\u73b0\uff0c\u53ea\u662f\u4f7f\u7528\u8be5\u51fd\u6570\u9700\u8981\u6ce8\u610f\uff0c\u5728\u505a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u65f6\u5019\u5bb9\u6613\u51fa\u73b0\u7684\u91cd\u590d\u3001\u9519\u8bef\u5339\u914d\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u4e2d\u5b58\u5728&#8221;zhuge&#8221;\uff0c\u90a3\u4e48\u5339\u914d&#8221;zhu&#8221;\uff0c\u5219\u53ef\u80fd\u5339\u914d\u6210\u529f\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662f\u671f\u671b\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u8868\uff1a<\/h3>\n\n\n\n<p>\u672c\u6587\u4e2d\u4f7f\u7528\u7684\u4e00\u4e9b\u793a\u4f8b\u8868\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE TABLE t_01(\n  id int UNSIGNED AUTO_INCREMENT primary key,\n  u_name varchar(32)\n);\n\nINSERT INTO t_01(u_name) values\n  ('wu'),\n  ('zhao'),\n  ('qian'),\n  ('sun'),\n  ('li');<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u65e5\u5e38\u7f16\u5199SQL\u4e2d\uff0c\u4f7f\u7528\u53d8\u91cf\u6765\u7075\u6d3b\u7684\u6784\u5efa\u6267\u884c\u7684SQL\u8bed\u53e5\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u3002\u5c31\u50cf\u5728\u7f16\u7a0b\u4e2d\u4f7f\u7528\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u7b49\u662f\u4e00\u6837\u7684\u9053\u7406\u3002\u672c\u6587\u4ecb\u7ecd\u4f7f\u7528JSON\u51fd\u6570\uff08JSON_ARRAY\u3001JSON_SEARCH\uff09\u3001\u4ee5\u53ca FIND_IN_SET \u5982\u4f55\u7b80\u5355\u7684\u5b9e\u73b0\u5e26\u6709\u6570\u636e\u7ec4\u53d8\u91cf\u7684SQL\u62fc\u63a5\u4e0e\u6267\u884c\u3002\u4f7f\u7528\u8be5\u65b9\u6cd5\uff0c\u5219\u65e0\u9700\u4f7f\u7528CONCAT\u51fd\u6570\u62fc\u63a5SQL\uff0c\u518d\u4f7f\u7528PREPARE\/EXECUTE\u8bed\u6cd5\u6267\u884c\uff0c\u6240\u4ee5\u4f1a\u66f4\u52a0\u7b80\u6d01\uff0c\u53ef\u8bfb\u6027\u3001\u6269\u5c55\u6027\u66f4\u5f3a\u3002 \u5177\u4f53\u7684\uff0c\u4f8b\u5982\u9700\u8981\u518d\u67e5\u8be2\u7ed3\u679c\u4e2d\u627e\u5230u_name\u5728\u6570\u7ec4@u_list_j\u4e2d\u7684\u8bb0\u5f55\uff0c\u90a3\u4e48\u4f7f\u7528JSON\u65b9\u5f0f\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u6a21\u5f0f\u5b9e\u73b0\uff1a \u53e6\u4e00\u79cd\u8f83\u4e3a\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 FIND_IN_SET \u51fd\u6570\u3002\u5176\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a \u53e6\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528 LOCATE \u51fd\u6570\u5b9e\u73b0\uff0c\u53ea\u662f\u4f7f\u7528\u8be5\u51fd\u6570\u9700\u8981\u6ce8\u610f\uff0c\u5728\u505a\u5b57\u7b26\u4e32\u5339\u914d\u7684\u65f6\u5019\u5bb9\u6613\u51fa\u73b0\u7684\u91cd\u590d\u3001\u9519\u8bef\u5339\u914d\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u4e2d\u5b58\u5728&#8221;zhuge&#8221;\uff0c\u90a3\u4e48\u5339\u914d&#8221;zhu&#8221;\uff0c\u5219\u53ef\u80fd\u5339\u914d\u6210\u529f\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662f\u671f\u671b\u7684\u7ed3\u679c\u3002 \u793a\u4f8b\u8868\uff1a \u672c\u6587\u4e2d\u4f7f\u7528\u7684\u4e00\u4e9b\u793a\u4f8b\u8868\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"a-temp-temp","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6],"tags":[],"class_list":["post-8385","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/8385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=8385"}],"version-history":[{"count":13,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/8385\/revisions"}],"predecessor-version":[{"id":8483,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/8385\/revisions\/8483"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=8385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=8385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=8385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}