{"id":412,"date":"2016-11-05T10:43:01","date_gmt":"2016-11-05T10:43:01","guid":{"rendered":"http:\/\/www.nywzjs.com\/?p=412"},"modified":"2017-12-06T03:06:34","modified_gmt":"2017-12-06T03:06:34","slug":"mysql-%e7%9a%84-find_in_set%e5%87%bd%e6%95%b0%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.nywzjs.com\/?p=412","title":{"rendered":"mysql \u7684 find_in_set\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>\u4e3e\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff1a<br \/>\n\u6709\u4e2a\u6587\u7ae0\u8868\u91cc\u9762\u6709\u4e2atype\u5b57\u6bb5,\u4ed6\u5b58\u50a8\u7684\u662f\u6587\u7ae0\u7c7b\u578b,\u6709 1\u5934\u6761,2\u63a8\u8350,3\u70ed\u70b9,4\u56fe\u6587 .....11,12,13\u7b49\u7b49<\/p>\n<p>\u73b0\u5728\u6709\u7bc7\u6587\u7ae0\u4ed6\u65e2\u662f \u5934\u6761,\u53c8\u662f\u70ed\u70b9,\u8fd8\u662f\u56fe\u6587,<\/p>\n<p>type\u4e2d\u4ee5 1,3,4\u7684\u683c\u5f0f\u5b58\u50a8.<\/p>\n<p>\u4eec\u6211\u4eec\u5982\u4f55\u7528sql\u67e5\u627e\u6240\u6709type\u4e2d\u67094\u56fe\u6587\u6807\u51c6\u7684\u6587\u7ae0\u5462\uff1f\uff1f<br \/>\n\u8fd9\u5c31\u8981\u6211\u4eec\u7684find_in_set\u51fa\u9a6c\u7684\u65f6\u5019\u5230\u4e86.<\/p>\n<p>\u4ee5\u4e0b\u4e3a\u5f15\u7528\u7684\u5185\u5bb9\uff1a<\/p>\n<pre class=\"prettyprint\">select * from article where FIND_IN_SET('4',type)<\/pre>\n<p>----------------------------------------------------------<br \/>\nmysql\u624b\u518c\u4e2dfind_in_set\u51fd\u6570\u7684\u8bed\u6cd5:<\/p>\n<p>FIND_IN_SET(str,strlist)<\/p>\n<p>\u5047\u5982\u5b57\u7b26\u4e32str \u5728\u7531N \u5b50\u94fe\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u5217\u8868strlist \u4e2d\uff0c\u5219\u8fd4\u56de\u503c\u7684\u8303\u56f4\u5728 1 \u5230 N \u4e4b\u95f4\u3002<br \/>\n\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868\u5c31\u662f\u4e00\u4e2a\u7531\u4e00\u4e9b\u88ab\u2018,\u2019\u7b26\u53f7\u5206\u5f00\u7684\u5b50\u94fe\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5e38\u6570\u5b57\u7b26\u4e32\uff0c\u800c\u7b2c\u4e8c\u4e2a\u662ftype SET\u5217\uff0c\u5219 FIND_IN_SET() \u51fd\u6570\u88ab\u4f18\u5316\uff0c\u4f7f\u7528\u6bd4\u7279\u8ba1\u7b97\u3002<br \/>\n\u5982\u679cstr\u4e0d\u5728strlist \u6216strlist \u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de\u503c\u4e3a 0 \u3002\u5982\u4efb\u610f\u4e00\u4e2a\u53c2\u6570\u4e3aNULL\uff0c\u5219\u8fd4\u56de\u503c\u4e3a NULL\u3002\u8fd9\u4e2a\u51fd\u6570\u5728\u7b2c\u4e00\u4e2a\u53c2\u6570\u5305\u542b\u4e00\u4e2a\u9017\u53f7(\u2018,\u2019)\u65f6\u5c06\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p>mysql&gt; SELECT FIND_IN_SET('b','a,b,c,d');<\/p>\n<p>-&gt; 2 \u56e0\u4e3ab \u5728strlist\u96c6\u5408\u4e2d\u653e\u57282\u7684\u4f4d\u7f6e \u4ece1\u5f00\u59cb<br \/>\n--------------------------------------------------------<br \/>\nselect FIND_IN_SET('1','1'); \u8fd4\u56de \u5c31\u662f1 \u8fd9\u65f6\u5019\u7684strlist\u96c6\u5408\u6709\u70b9\u7279\u6b8a \u53ea\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32 \u5176\u5b9e\u5c31\u662f\u8981\u6c42\u524d\u4e00\u4e2a\u5b57\u7b26\u4e32 \u4e00\u5b9a\u8981\u5728\u540e\u4e00\u4e2a\u5b57\u7b26\u4e32\u96c6\u5408\u4e2d \u624d\u8fd4\u56de \u5927\u4e8e0\u7684\u6570<\/p>\n<p>select FIND_IN_SET('2','1\uff0c2');\u8fd4\u56de2<br \/>\nselect FIND_IN_SET('6','1'); \u8fd4\u56de0<br \/>\n----------------------------------------------------------<\/p>\n<p>\u6ce8\u610f\uff1a<br \/>\nselect * from treenodes where FIND_IN_SET(id,'1,2,3,4,5');<br \/>\n\u4f7f\u7528find_in_set\u51fd\u6570\u4e00\u6b21\u8fd4\u56de\u591a\u6761\u8bb0\u5f55<\/p>\n<p>id \u662f\u4e00\u4e2a\u8868\u7684\u5b57\u6bb5 \u7136\u540e\u6bcf\u6761\u8bb0\u5f55\u5206\u522b\u662fid\u7b49\u4e8e1\uff0c2\uff0c3\uff0c4\uff0c5\u7684\u65f6\u5019<br \/>\n\u6709\u70b9\u7c7b\u4f3cin \uff08\u96c6\u5408\uff09<br \/>\nselect * from treenodes where id in (1,2,3,4,5);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3e\u4e2a\u4f8b\u5b50\u6765\u8bf4\uff1a \u6709\u4e2a\u6587\u7ae0\u8868\u91cc\u9762\u6709\u4e2atype\u5b57\u6bb5,\u4ed6\u5b58\u50a8\u7684\u662f\u6587\u7ae0\u7c7b\u578b,\u6709 1\u5934\u6761,2\u63a8\u8350,3\u70ed\u70b9,4\u56fe\u6587 &#8230; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[82,45],"_links":{"self":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/posts\/412"}],"collection":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":0,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}