<?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>fatkun&#039;s blog &#187; hibernate子查询</title>
	<atom:link href="http://fatkun.com/tag/hibernate%e5%ad%90%e6%9f%a5%e8%af%a2/feed" rel="self" type="application/rss+xml" />
	<link>http://fatkun.com</link>
	<description>又一个 WordPress 站点</description>
	<lastBuildDate>Sat, 19 May 2012 17:25:31 +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>如何在SQL中先排序后分组</title>
		<link>http://fatkun.com/2010/07/group-by-and-order-by.html</link>
		<comments>http://fatkun.com/2010/07/group-by-and-order-by.html#comments</comments>
		<pubDate>Sat, 03 Jul 2010 13:50:44 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[group by]]></category>
		<category><![CDATA[hibernate子查询]]></category>
		<category><![CDATA[order by]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=427</guid>
		<description><![CDATA[标题说的有点奇怪，换句话说是让order by比group by先执行。还不明白对吧？ 举个例子：实现场景，要实现QQ空间的动态消息，首先要按最新的消息查到QQ好友排序，然后再按好友分别查询他们的动态消息 有如下的Feed表 id userid type msg who time 1 2 add_twitter hahah 1 2010-04-27 19:12:38 2 2 add_twitter 怎么啦 2 2010-04-27 19:12:44 3 4 add_twitter asddas 3 2010-04-27 19:20:26 4 2 reply_twitter 放大法 4 2010-04-27 19:24:09 5 5 reply_twitter 噶 5 2010-04-27 19:24:13 6 3 add_twitter saf 6 2010-04-27 19:35:48 7 2 [...]]]></description>
			<content:encoded><![CDATA[<p>标题说的有点奇怪，换句话说是让order by比group by先执行。还不明白对吧？<br />
举个例子：实现场景，要实现QQ空间的动态消息，首先要按最新的消息查到QQ好友排序，然后再按好友分别查询他们的动态消息</p>
<p>有如下的Feed表</p>
<pre>id	userid	type		msg	who	time
1	2	add_twitter		hahah	1	2010-04-27 19:12:38
2	2	add_twitter		怎么啦	2	2010-04-27 19:12:44
3	4	add_twitter		asddas	3	2010-04-27 19:20:26
4	2	reply_twitter		放大法	4	2010-04-27 19:24:09
5	5	reply_twitter		噶	5	2010-04-27 19:24:13
6	3	add_twitter		saf	6	2010-04-27 19:35:48
7	2	add_twitter		先谢谢谢谢谢谢	7	2010-04-27 19:54:44</pre>
<p>现在想要，按时间先后，把用户的id查询出来，每个id只出现一次。</p>
<p>当然，第一时间就想到用group by语句来分组嘛</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> feed <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> userid <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">TIME</span></pre></div></div>

<p>可是，得到的结果并不是自己想要的，group by比order by先解析了，结果就是得到最旧的那一条。</p>
<h2>正确的方法</h2>
<p>使用子查询</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> feed <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> T 
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> T<span style="color: #66cc66;">.</span>userid <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> T<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TIME</span></pre></div></div>

<p>上面这句在mysql可以正常取值，但是在hibernate不能使用from 后面的子查询，杯具。。<br />
换个折中的方法，以下方法只是为了取得userid而已</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> feed <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> userid <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/group-by-and-order-by.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

