<?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>迷途知返 &#187; SQL</title>
	<atom:link href="http://pwwang.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://pwwang.com</link>
	<description>专注技术,用心生活!</description>
	<lastBuildDate>Wed, 08 Feb 2012 09:37:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>随机记录分页</title>
		<link>http://pwwang.com/2011/05/20/random-records-pagination/</link>
		<comments>http://pwwang.com/2011/05/20/random-records-pagination/#comments</comments>
		<pubDate>Fri, 20 May 2011 05:38:04 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[分页]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=1037</guid>
		<description><![CDATA[&#160;

问题（需求）：

1. 每次打开第一页时，显示的记录不同。

<span class="readmore"><a href="http://pwwang.com/2011/05/20/random-records-pagination/" title="随机记录分页">Read More: 3925 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>问题（需求）：</p>
<p>1. 每次打开第一页时，显示的记录不同。</p>
<p>2. 但是从第一页后的其他页面（第二页，第三页，。。。）返回第一页时，还是那个第一次打开第一页的那些记录</p>
<p>3. 每页记录不能重复</p>
<p>&nbsp;</p>
<p>其实看起来很简单的一个问题，但是加上分页的要求之后，变得稍微有点复杂。</p>
<p>如果是随机从数据库取一条记录，最直接的方式，就是order by rand() limit 1:</p>
<p><span id="more-1037"></span></p>
<p>当然，网上也有很多提高order by rand()效率的写法，都可以参考。</p>
<p>如果是不需要分页，那order by rand()及其优化方式，也都可以满足需求。</p>
<p>倘若需要分页，我们给rand指定一个参数，再配以limit，可以满足我们第2，3条要求。</p>
<p>因为如果每次请求rand的参数是不变的，那每次取的记录顺序也是一样的，这样每次每开第一页的记录，自然也是一样的。</p>
<p>重新连接数据库也一样。</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection <span style="color: #c20cb9; font-weight: bold;">id</span> is <span style="color: #000000;">3</span>
Server version: 5.5.8 Source distribution
&nbsp;
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2000</span>, <span style="color: #000000;">2010</span>, Oracle and<span style="color: #000000; font-weight: bold;">/</span>or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is <span style="color: #c20cb9; font-weight: bold;">free</span> software,
and you are welcome to modify and redistribute it under the GPL v2 license
&nbsp;
Type <span style="color: #ff0000;">'help;'</span> or <span style="color: #ff0000;">'\h'</span> <span style="color: #000000; font-weight: bold;">for</span> help. Type <span style="color: #ff0000;">'\c'</span> to <span style="color: #c20cb9; font-weight: bold;">clear</span> the current input statement.
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> use database;
Reading table information <span style="color: #000000; font-weight: bold;">for</span> completion of table and column names
You can turn off this feature to get a quicker startup with <span style="color: #660033;">-A</span>
&nbsp;
Database changed
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> <span style="color: #c20cb9; font-weight: bold;">id</span> from table order by rand<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+----+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">id</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">7</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">9</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000;">8</span> rows <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> Bye
pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection <span style="color: #c20cb9; font-weight: bold;">id</span> is <span style="color: #000000;">4</span>
Server version: 5.5.8 Source distribution
&nbsp;
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2000</span>, <span style="color: #000000;">2010</span>, Oracle and<span style="color: #000000; font-weight: bold;">/</span>or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is <span style="color: #c20cb9; font-weight: bold;">free</span> software,
and you are welcome to modify and redistribute it under the GPL v2 license
&nbsp;
Type <span style="color: #ff0000;">'help;'</span> or <span style="color: #ff0000;">'\h'</span> <span style="color: #000000; font-weight: bold;">for</span> help. Type <span style="color: #ff0000;">'\c'</span> to <span style="color: #c20cb9; font-weight: bold;">clear</span> the current input statement.
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> use database;
Reading table information <span style="color: #000000; font-weight: bold;">for</span> completion of table and column names
You can turn off this feature to get a quicker startup with <span style="color: #660033;">-A</span>
&nbsp;
Database changed
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> <span style="color: #c20cb9; font-weight: bold;">id</span> from table order by rand<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+----+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">id</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">7</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">9</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000;">8</span> rows <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> Bye</pre></div></div>

<p>&nbsp;</p>
<p>重启数据库，结果还是一样</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> lampp restart
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #c20cb9; font-weight: bold;">sudo</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> password <span style="color: #000000; font-weight: bold;">for</span> pwwang:
Stopping XAMPP <span style="color: #000000; font-weight: bold;">for</span> Linux 1.7.4...
XAMPP: Stopping Apache with SSL...
XAMPP: Stopping MySQL...
XAMPP: Stopping ProFTPD...
XAMPP stopped.
Starting XAMPP <span style="color: #000000; font-weight: bold;">for</span> Linux 1.7.4...
XAMPP: Starting Apache with SSL <span style="color: #7a0874; font-weight: bold;">&#40;</span>and PHP5<span style="color: #7a0874; font-weight: bold;">&#41;</span>...
XAMPP: Starting MySQL...
XAMPP: Starting ProFTPD...
XAMPP <span style="color: #000000; font-weight: bold;">for</span> Linux started.
pwwang<span style="color: #000000; font-weight: bold;">@</span>pwwang-xxx:~$ mysql <span style="color: #660033;">-uroot</span> <span style="color: #660033;">-p</span>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection <span style="color: #c20cb9; font-weight: bold;">id</span> is <span style="color: #000000;">1</span>
Server version: 5.5.8 Source distribution
&nbsp;
Copyright <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">2000</span>, <span style="color: #000000;">2010</span>, Oracle and<span style="color: #000000; font-weight: bold;">/</span>or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is <span style="color: #c20cb9; font-weight: bold;">free</span> software,
and you are welcome to modify and redistribute it under the GPL v2 license
&nbsp;
Type <span style="color: #ff0000;">'help;'</span> or <span style="color: #ff0000;">'\h'</span> <span style="color: #000000; font-weight: bold;">for</span> help. Type <span style="color: #ff0000;">'\c'</span> to <span style="color: #c20cb9; font-weight: bold;">clear</span> the current input statement.
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> use database;
Reading table information <span style="color: #000000; font-weight: bold;">for</span> completion of table and column names
You can turn off this feature to get a quicker startup with <span style="color: #660033;">-A</span>
&nbsp;
Database changed
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> <span style="color: #c20cb9; font-weight: bold;">id</span> from table order by rand<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+----+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">id</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">4</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">7</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">9</span> <span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">|</span>  <span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">|</span>
+----+
<span style="color: #000000;">8</span> rows <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>&nbsp;</p>
<p>因为给定参数之后，RAND的结果是一样的</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>             <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000;">0.40540353712197724</span> <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000;">1</span> row <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.02</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">select</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> RAND<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>             <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000;">0.40540353712197724</span> <span style="color: #000000; font-weight: bold;">|</span>
+---------------------+
<span style="color: #000000;">1</span> row <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>&nbsp;</p>
<p>如果我们每次在打开第一页的时候就给RAND一个随机的参数，那问题就可以解决了。</p>
<p>&nbsp;</p>
<p><strong><span style="color: rgb(0, 128, 128);"><span style="font-size: larger;">Solution 1：</span></span></strong></p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$seed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'seed'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 你也可以用session, cookie等方式来存储这个值</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'seed'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
    <span style="color: #000088;">$seed</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id FROM table ORDER BY RAND(<span style="color: #006699; font-weight: bold;">$seed</span>) LIMIT <span style="color: #006699; font-weight: bold;">$offset</span>, <span style="color: #006699; font-weight: bold;">$limit</span>;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&nbsp;</p>
<p>页面渲染的时候每个和分页相关的链接都带上参数seed=$seed, 就可以保证在这个周期的记录浏览中，每一页的记录都是特定的。</p>
<p>在下个周期中，由于没有$_GET['seed']的值，$seed会重新随机取值，那么，第一页的记录也就和上个周期不一样了。</p>
<p>&nbsp;</p>
<p><strong><span style="color: rgb(0, 128, 128);"><span style="font-size: larger;">Solution 2：</span></span></strong></p>
<p>当然，也有人说到了order by rand() 的效率问题。那我们可以用脚本事先把primary key（假设这里是increment的id）排好随机顺序，再传给sql去查询。</p>
<p>如果你的记录的id是连续的，你可以用max(id)来获取pk的序列：</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'serial'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$serial</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$maxid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_value</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT MAX(id) FROM table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$serial</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$ids_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$serial</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ids_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id FROM table WHERE id IN (<span style="color: #006699; font-weight: bold;">$ids</span>);&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如果你单页的记录不多的话（一般也不会太多），这个替代方案可以很好的弥补order by rand的效率问题</p>
<p>&nbsp;</p>
<p>但是如果你的记录id不是连续的，那么就想要办法先拿到id的序列：</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_value</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT group_concat(id) FROM table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$serial</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这里只演示了如何获取id序列。其它机制和连续id是一样的。</p>
<p>&nbsp;</p>
<p>不过这里用到了group_concat，来选取所有记录的id。</p>
<p>效率问题，大家可以根据实际情况在order by rand和group_concat之间权衡。</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2011/05/20/random-records-pagination/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>关于SQL的一道笔试题</title>
		<link>http://pwwang.com/2009/11/28/a_question_about_sql/</link>
		<comments>http://pwwang.com/2009/11/28/a_question_about_sql/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 17:44:14 +0000</pubDate>
		<dc:creator>pwwang</dc:creator>
				<category><![CDATA[Techniques]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://pwwang.com/?p=834</guid>
		<description><![CDATA[&#160;

题目要求是这样的，假设有如下表（MySQL）：

TABLE: test

<span class="readmore"><a href="http://pwwang.com/2009/11/28/a_question_about_sql/" title="关于SQL的一道笔试题">Read More: 582 Words Totally</a></span>]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>题目要求是这样的，假设有如下表（MySQL）：</p>
<p>TABLE: test</p>
<table cellspacing="1" cellpadding="1" border="0" style="background-color: rgb(153, 153, 253); width: 166px; height: 163px; clear: both;">
<tbody>
<tr style="background-color: rgb(255, 255, 255);">
<td><strong>id</strong></td>
<td><strong>a</strong></td>
<td><strong>b</strong></td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>1</td>
<td>4</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>2</td>
<td>1</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>3</td>
<td>1</td>
<td>3</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>4</td>
<td>3</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>5</td>
<td>1</td>
<td>5</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>6</td>
<td>1</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>7</td>
<td>4</td>
<td>-1</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>8</td>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
<p>&nbsp;写一条SQL语句，</p>
<p>选择所有a=1或b=2的记录，</p>
<p>使得<span style="color: rgb(255, 0, 0);">a=1且b=2</span>的记录排在最前面，</p>
<p>并且a=1且b=2的记录按<span style="color: rgb(255, 0, 0);">id降序</span>排列。</p>
<p><span id="more-834"></span>分析：</p>
<p>很显然，直接的order by方案是不行的。</p>
<p>那我们就需要把条件分割：把a=1 or b=2分割成（a=1 and b=2） or （a=1 and b!=2） or （a!=1 and b=2）</p>
<p>再把选择的结果union一下就可以得到想要的结果了。</p>
<p>关于（a=1 and b=2）记录按id的降序排列，需要用到order by，如果直接这么用：</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">...</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">...</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">...</span> <span style="color: #993333; font-weight: bold;">DESC</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #66cc66;">...</span></pre></div></div>

<p>是会报错的， 把前面的select语句加上括号，不会报错，但是达不到想要的效果（order by 不起作用）。</p>
<p>&nbsp;</p>
<p>这个时候就要用到derived table了，不过别忘了给derived table取一个别名（alias）。</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`test`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`a`</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">`b`</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">DESC</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">`ab`</span> <span style="color: #993333; font-weight: bold;">UNION</span> 
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`test`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`a`</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">`b`</span>!<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`a`</span>!<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">`b`</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>&nbsp;运行结果：</p>
<table cellspacing="1" cellpadding="1" border="0" style="background-color: rgb(255, 153, 0); width: 166px; height: 163px; clear: both;">
<tbody>
<tr style="background-color: rgb(255, 255, 255);">
<td><strong>id</strong></td>
<td><strong>a</strong></td>
<td><strong>b</strong></td>
</tr>
<tr style="background-color: rgb(200, 200, 200);">
<td>8</td>
<td>1</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(200, 200, 200);">
<td>6</td>
<td>1</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(200, 200, 200);">
<td>2</td>
<td>1</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>1</td>
<td>4</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>3</td>
<td>1</td>
<td>3</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>4</td>
<td>3</td>
<td>2</td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>5</td>
<td>1</td>
<td>5</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://pwwang.com/2009/11/28/a_question_about_sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

