<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FIND_IN_SET &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/find_in_set/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Wed, 20 Mar 2013 07:49:52 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>Mysql的find_in_set函数查询与like查询</title>
		<link>https://www.mudbest.com/mysql%e7%9a%84find_in_set%e5%87%bd%e6%95%b0%e6%9f%a5%e8%af%a2%e4%b8%8elike%e6%9f%a5%e8%af%a2/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Wed, 20 Mar 2013 07:40:18 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[FIND_IN_SET]]></category>
		<category><![CDATA[instr()]]></category>
		<category><![CDATA[like]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1822</guid>

					<description><![CDATA[对于mysql的like查询，我想都会很熟悉，对于一些基本查询也会常用，但始终不是对大数据量查询的方案。在查询...]]></description>
										<content:encoded><![CDATA[<p>对于mysql的like查询，我想都会很熟悉，对于一些基本查询也会常用，但始终不是对大数据量查询的方案。在查询数据需要对某一个字段里的值当作条件时，一般会用到like、instr()、find_in_set()，首先看一下手册里对这3种用法的定义。</p>
<p><strong>expr LIKE pat [ESCAPE &#8216;escape-char&#8217;] </strong></p>
<p>模式匹配，使用SQL简单正规表达式比较。返回1 (TRUE) 或 0 (FALSE)。 若 expr 或 pat 中任何一个为 NULL,则结果为 NULL。<br />
模式不需要为文字字符串。例如，可以被指定为一个字符串表达式或表列。<br />
在模式中可以同LIKE一起使用以下两种通配符： <span id="more-1822"></span><br />
字符 说明<br />
 %   匹配任何数目的字符，甚至包括零字符<br />
 _   只能匹配一种字符</p>
<p><strong>FIND_IN_SET(str,strlist) </strong></p>
<p>假如字符串str 在由N 子链组成的字符串列表strlist 中， 则返回值的范围在 1 到 N 之间 。一个字符串列表就是一个由一些被‘,’符号分开的自链组成的字符串。如果第一个参数是一个常数字符串，而第二个是type SET列，则   FIND_IN_SET() 函数被优化，使用比特计算。如果str不在strlist 或strlist 为空字符串，则返回值为 0 。如任意一个参数为NULL，则返回值为 NULL。 这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。</p>
<p><strong>INSTR(str,substr)   </strong></p>
<p>返回字符串 str 中子字符串的第一个出现位置。这和LOCATE()的双参数形式相同，除非参数的顺序被颠倒。 </p>
<p>然后来实际操作一次，有以下一下这样的一个数据。</p>
<p>data：</p>
<pre class="brush: sql; title: ; notranslate">
+------+----------------+
| id   | tagsid         |
+------+----------------+
|    4 | 2863,4351,4353 |
|    3 |                |
|    5 | 3395,2219      |
|    6 |                |
|    8 |                |
|    9 |                |
|   10 | 2689,4349,2863 |
| 1652 |                |
|   12 | 4347,3161,3029 |
|   13 | 2193,2519,2229 |
+------+----------------+
10 rows in set (0.00 sec)
</pre>
<p>首先我们使用like查询，结果如下：</p>
<pre class="brush: sql; title: ; notranslate">
mysql&gt; select id,tagsid from tbl_news where find_in_set('2863',`tagsid`) limit 1
0;
+-----+--------------------------+
| id  | tagsid                   |
+-----+--------------------------+
|   4 | 2863,4351,4353           |
|  10 | 2689,4349,2863           |
|  78 | 2863,2197,4327           |
| 245 | 3395,2863                |
|  85 | 2863,2497,3869,4315      |
| 108 | 2193,4301,2863,4303      |
| 121 | 4277,2863,4279,3311,3215 |
| 122 | 4277,4279,2863,4189,3109 |
| 137 | 3361,3811,2829,3255,2863 |
| 149 | 2289,2197,2863,2519,4199 |
+-----+--------------------------+
10 rows in set (0.00 sec)
</pre>
<p>注意：<br />
select id,tagsid from tbl_news where find_in_set(字段,值) limit 10;<br />
使用find_in_set函数一次返回多条记录 </p>
<pre class="brush: sql; title: ; notranslate">
mysql&gt; select id,tagsid from tbl_news where find_in_set(`id`,'78,10,21,2,3,22') limit 10;
</pre>
<p>id 是一个表的字段 然后每条记录分别是id等于78,10,21,2,3,22的时候，有点类似in （集合）。</p>
<pre class="brush: sql; title: ; notranslate">
mysql&gt; select id,tagsid from tbl_news where `id` in(78,10,21,2,3,22) limit 10;
</pre>
<p>然后用find_in_set()查询，结果如下：</p>
<pre class="brush: sql; title: ; notranslate">
mysql&gt; select id,tagsid from `tbl_news` limit 10;
+------+----------------+
| id   | tagsid         |
+------+----------------+
|    4 | 2863,4351,4353 |
|    3 |                |
|    5 | 3395,2219      |
|    6 |                |
|    8 |                |
|    9 |                |
|   10 | 2689,4349,2863 |
| 1652 |                |
|   12 | 4347,3161,3029 |
|   13 | 2193,2519,2229 |
+------+----------------+
10 rows in set (0.00 sec)
</pre>
<p>最后使用instr()查询，结果如下：</p>
<pre class="brush: sql; title: ; notranslate">
mysql&gt; select id,tagsid from tbl_news where instr(`tagsid`,'2863') &gt; 0 limit 10;

+-----+--------------------------+
| id  | tagsid                   |
+-----+--------------------------+
|   4 | 2863,4351,4353           |
|  10 | 2689,4349,2863           |
|  78 | 2863,2197,4327           |
| 245 | 3395,2863                |
|  85 | 2863,2497,3869,4315      |
| 108 | 2193,4301,2863,4303      |
| 121 | 4277,2863,4279,3311,3215 |
| 122 | 4277,4279,2863,4189,3109 |
| 137 | 3361,3811,2829,3255,2863 |
| 149 | 2289,2197,2863,2519,4199 |
+-----+--------------------------+
10 rows in set (0.00 sec)
</pre>
<p>初步查看并没有什么区别，紧紧在使用方法有所不同，由于查询量比较小，也未做压力测试，查询效率也就不曾而知了，但对于用法上，可根据自己的需求，选择性使用。对于大数据的查询，建议还是数据层上面搭建一个缓冲层，避免对数据库的实时查询等操作，比如索引，缓存，等等措施，毕竟全文查询对mysql压力实在是太大，以上均为本人拙见。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Mysql的FIND_IN_SET优化使用</title>
		<link>https://www.mudbest.com/mysql%e7%9a%84find_in_set%e4%bc%98%e5%8c%96%e4%bd%bf%e7%94%a8/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Tue, 12 Apr 2011 10:36:17 +0000</pubDate>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[FIND_IN_SET]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=583</guid>

					<description><![CDATA[假如字符串str 在由N 子链组成的字符串列表strlist 中， 则返回值的范围在 1 到 N 之间 。一个...]]></description>
										<content:encoded><![CDATA[<p>假如字符串str 在由N 子链组成的字符串列表strlist 中， 则返回值的范围在 1 到 N 之间 。一个字符串列表就是一个由一些被‘,’符号分开的自链组成的字符串。如果第一个参数是一个常数字符串，而第二个是type SET列，则   FIND_IN_SET() 函数被优化，使用比特计算。如果str不在strlist 或strlist 为空字符串，则返回值为 0 。如任意一个参数为NULL，则返回值为 NULL。 这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。  </p>
<p>mysql> SELECT FIND_IN_SET(&#8216;b&#8217;,&#8217;a,b,c,d&#8217;);</p>
<p>例：<span id="more-583"></span></p>
<pre class="brush: sql; title: ; notranslate">
$sql = &quot;SELECT aid,title,litpic FROM `&amp;*_addonhqimages` AS a INNER JOIN `&amp;*_archives` AS b ON a.aid = b.id WHERE b.arcrank = 0 AND FIND_IN_SET('c',b.flag) ORDER BY b.pubdate DESC&quot;;
</pre>
<p>再或者例</p>
<pre class="brush: php; title: ; notranslate">
$sql = SELECT * FROM 表名 as `别名` where FIND_IN_SET('字段中的一个值',`别名`.`字段`) &gt; 0 AND FIND_IN_SET('字段中的一个值',`别名`.`字段`) &gt; 0
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
