<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[阿Tim日志]]></title> 
<link>https://atim.cn/index.php</link> 
<description><![CDATA[专业的php开发者.开发团队的带队人]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[阿Tim日志]]></copyright>
<item>
<link>https://atim.cn/Mysql-group-by-and-order-by-research/</link>
<title><![CDATA[mysql “group by ”与order by的研究－－分类中最新的内容]]></title> 
<author>bkkkd &lt;partybase@gmail.com&gt;</author>
<category><![CDATA[开发应用]]></category>
<pubDate>Mon, 26 Mar 2012 01:53:54 +0000</pubDate> 
<guid>https://atim.cn/Mysql-group-by-and-order-by-research/</guid> 
<description>
<![CDATA[ 
	<div class="quote"><div class="quote-title">引用</div><div class="quote-content">之前掉失的日志，还好有人引用了。不然我也找不到了。</div></div><br/>这两天让一个数据查询难了。主要是对group by 理解的不够深入。才出现这样的情况<br/>这种需求，我想很多人都遇到过。下面是我模拟我的内容表.<br/><a href="https://atim.cn/attachment.php?fid=41" target="_blank"><img src="https://atim.cn/attachment.php?fid=41" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><span style="color: red;"><strong>我现在需要取出每个分类中最新的内容</strong></span><br/><div class="code">select * from test group by category_id order by `date`</div><br/>结果如下<br/><a href="https://atim.cn/attachment.php?fid=42" target="_blank"><img src="https://atim.cn/attachment.php?fid=42" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>明显。这不是我想要的数据，原因是msyql已经的执行顺序是<br/>写的顺序：<br/><div class="code">select ... from... where.... group by... having... order by..</div><br/>执行顺序：<br/><div class="code">from... where...group by... having.... select ... order by...</div><br/>所以在order by拿到的结果里已经是分组的完的最后结果。<br/>由from到where的结果如下的内容。<br/><div class="code">select * from test where 1</div><br/><a href="https://atim.cn/attachment.php?fid=41" target="_blank"><img src="https://atim.cn/attachment.php?fid=41" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>到group by时就得到了根据category_id分出来的多个小组<br/><br/><div class="code">select * from test where category_id=1</div><br/><a href="https://atim.cn/attachment.php?fid=43" target="_blank"><img src="https://atim.cn/attachment.php?fid=43" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><div class="code">select * from test where category_id=2</div><br/><a href="https://atim.cn/attachment.php?fid=44" target="_blank"><img src="https://atim.cn/attachment.php?fid=44" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>到了select的时候，只从上面的每个组里取第一条信息结果会如下<br/><div class="code">select * from test where 1 group by category_id</div><br/><a href="https://atim.cn/attachment.php?fid=45" target="_blank"><img src="https://atim.cn/attachment.php?fid=45" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>即使order by也只是从上面的结果里进行排序。并不是每个分类的最新信息。<br/>回到我的目的上 －－分类中最新的信息<br/>根据上面的分析，group by到select时只取到分组里的第一条信息。有两个解决方法<br/>1，where+group by（对小组进行排序）<br/>2，从form返回的数据下手脚（即用子查询）<br/><br/><strong>由where+group by的解决方法</strong><br/>对group by里的小组进行排序的函数我只查到group_concat()可以进行排序，但group_concat的作用是将小组里的字段里的值进行串联起来。<br/><div class="code">select group_concat(id order by `date` desc) from `test` group by category_id</div><br/><a href="https://atim.cn/attachment.php?fid=46" target="_blank"><img src="https://atim.cn/attachment.php?fid=46" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><div class="code">select * from `test` where id in(select SUBSTRING_INDEX(group_concat(id order by `date` desc),&#039;,&#039;,1) from `test` group by category_id ) order by `date` desc</div><br/><a href="https://atim.cn/attachment.php?fid=47" target="_blank"><img src="https://atim.cn/attachment.php?fid=47" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><strong>子查询解决方案</strong><br/><div class="code">select * from (select * from `test` order by `date` desc) `temp`&nbsp;&nbsp;group by category_id order by `date` desc</div><br/><a href="https://atim.cn/attachment.php?fid=47" target="_blank"><img src="https://atim.cn/attachment.php?fid=47" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><hr/><br/>2014-1-12网友：skywalker_lan 提出子查询解决方案最后面增加order by是没有作用。基于研究精神，还是测试一次。<br/>附上测试用的数据表sql<br/><div class="code">CREATE TABLE `test` (&nbsp;&nbsp;`id` INT(10) NOT NULL AUTO_INCREMENT,&nbsp;&nbsp;`name` CHAR(50) NOT NULL,&nbsp;&nbsp;`category_id` INT(10) NOT NULL,&nbsp;&nbsp;`date` DATETIME NOT NULL,&nbsp;&nbsp;PRIMARY KEY (`id`))COLLATE=&#039;utf8_general_ci&#039;ENGINE=MyISAM;REPLACE INTO `test` (`id`, `name`, `category_id`, `date`) VALUES (1, &#039;aaa&#039;, 1, &#039;2013-12-12 12:12:12&#039;);REPLACE INTO `test` (`id`, `name`, `category_id`, `date`) VALUES (2, &#039;bbb&#039;, 2, &#039;2013-12-13 12:12:12&#039;);REPLACE INTO `test` (`id`, `name`, `category_id`, `date`) VALUES (3, &#039;ccc&#039;, 1, &#039;2013-12-14 12:12:12&#039;);REPLACE INTO `test` (`id`, `name`, `category_id`, `date`) VALUES (4, &#039;ddd&#039;, 1, &#039;2013-12-15 12:12:12&#039;);REPLACE INTO `test` (`id`, `name`, `category_id`, `date`) VALUES (5, &#039;eee&#039;, 2, &#039;2013-12-16 12:12:12&#039;);</div><br/><a href="https://atim.cn/attachment.php?fid=51" target="_blank"><img src="https://atim.cn/attachment.php?fid=51" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>查询不带order by的数据：<br/><div class="code">select * from (select * from `test` order by `date` desc) `temp`&nbsp;&nbsp;group by category_id;</div><br/><a href="https://atim.cn/attachment.php?fid=49" target="_blank"><img src="https://atim.cn/attachment.php?fid=49" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>查询不带order by的数据：<br/><div class="code">select * from (select * from `test` order by `date` desc) `temp`&nbsp;&nbsp;group by category_id order by `date` desc;</div><br/><a href="https://atim.cn/attachment.php?fid=50" target="_blank"><img src="https://atim.cn/attachment.php?fid=50" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>具体原因没有看mysql的源码，所以不了解。不过结果很明显，子查询返回的结果是有排序，但group by后，排序发生改变（原因不详），所以才出现必须在后面再增加order by.<br/>Tags - <a href="https://atim.cn/tags/mysql/" rel="tag">mysql</a> , <a href="https://atim.cn/tags/sql/" rel="tag">sql</a> , <a href="https://atim.cn/tags/%25E8%25AF%25AD%25E6%25B3%2595/" rel="tag">语法</a>
]]>
</description>
</item><item>
<link>https://atim.cn/Mysql-group-by-and-order-by-research/#blogcomment5309</link>
<title><![CDATA[[评论] mysql “group by ”与order by的研究－－分类中最新的内容]]></title> 
<author>108918tcy &lt;popnsode74262@163.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sun, 13 May 2012 10:54:32 +0000</pubDate> 
<guid>https://atim.cn/Mysql-group-by-and-order-by-research/#blogcomment5309</guid> 
<description>
<![CDATA[ 
	途经看一下！！！！！！！！！！！！！
]]>
</description>
</item><item>
<link>https://atim.cn/Mysql-group-by-and-order-by-research/#blogcomment5348</link>
<title><![CDATA[[评论] mysql “group by ”与order by的研究－－分类中最新的内容]]></title> 
<author>evens205 &lt;kevin19910505@126.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Fri, 21 Sep 2012 09:01:09 +0000</pubDate> 
<guid>https://atim.cn/Mysql-group-by-and-order-by-research/#blogcomment5348</guid> 
<description>
<![CDATA[ 
	嗯，不错，分析的很好啊，以后多过来看看
]]>
</description>
</item><item>
<link>https://atim.cn/Mysql-group-by-and-order-by-research/#blogcomment6506</link>
<title><![CDATA[[评论] mysql “group by ”与order by的研究－－分类中最新的内容]]></title> 
<author>skywalker_lan &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 05 Dec 2013 03:16:24 +0000</pubDate> 
<guid>https://atim.cn/Mysql-group-by-and-order-by-research/#blogcomment6506</guid> 
<description>
<![CDATA[ 
	再来看一看，不过使用子查询的时候，外面的order by 应该不用了，而且也是不起作用的。<br/>select * from (select * from &#96;test&#96; order by &#96;date&#96; desc) &#96;temp&#96;&nbsp;&nbsp;group by category_id；<br/><br/>select * 中 没有聚合函数，select 也没有分组中的过滤条件 category_id, 所以这样严格来说应该也有问题。只不过mysql 在这钟情况下，默认把第一条数据返回到查询结果； 也就是分组之后各自得到一条数据；&nbsp;&nbsp;此时外面再加的 order by 应该没什么意义了。
]]>
</description>
</item>
</channel>
</rss>