<?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>涂0实验室 &#187; sql</title>
	<atom:link href="http://www.sulong.info/archives/tag/sql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sulong.info</link>
	<description>一个程序员的成长之路</description>
	<lastBuildDate>Fri, 27 Aug 2010 01:54:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>SQL select语句执行顺序</title>
		<link>http://www.sulong.info/archives/24</link>
		<comments>http://www.sulong.info/archives/24#comments</comments>
		<pubDate>Wed, 24 Oct 2007 15:04:09 +0000</pubDate>
		<dc:creator>sulong</dc:creator>
				<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://sulong.info/archives/24</guid>
		<description><![CDATA[先看以下的示例数据库表 T: id v c 1 2 3 2 3 3 3 4 5 其中id是主键。要求做如下操作：选取具有相同C的数据组中id最大的那一行的v值。经过思考，我得到如下查询语句：select id, v, c from T where id in ( select max(id) from T group by c);应查出结果： id v c 2 3 3 3 4 5 可惜现在使用的数据库是老旧的mysql 4.0，不支持子查询，不用子查询能实现这样的功能吗？我试了好多次都不能成功，比如：select max(id), id, v, c from T group by c;选出的结果为 max(id) id v c [...]]]></description>
			<content:encoded><![CDATA[<p>先看以下的示例数据库表 T:</p>
<p align="left">
<table id="nww:" height="102" cellspacing="0" cellpadding="3" width="412" bgcolor="#00cccc" border="1" unselectable="on">
<tbody>
<tr>
<td width="33%">id</td>
<td width="33%">v</td>
<td width="33%">c</td>
</tr>
<tr>
<td width="33%">1</td>
<td width="33%">2</td>
<td width="33%">3</td>
</tr>
<tr>
<td width="33%">2</td>
<td width="33%">3</td>
<td width="33%">3</td>
</tr>
<tr>
<td width="33%">3</td>
<td width="33%">4</td>
<td width="33%">5</td>
</tr>
</tbody>
</table>
<p>其中id是主键。要求做如下操作：选取具有相同C的数据组中id最大的那一行的v值。经过思考，我得到如下查询语句：<br />select id, v, c from T where id in ( select max(id) from T group by c);<br />应查出结果：</p>
<table id="tupt" height="77" cellspacing="0" cellpadding="3" width="415" bgcolor="#00cccc" border="1" unselectable="on">
<tbody>
<tr>
<td width="33%">id</td>
<td width="33%">v</td>
<td width="33%">c</td>
</tr>
<tr>
<td width="33%">2</td>
<td width="33%">3</td>
<td width="33%">3</td>
</tr>
<tr>
<td valign="top">3</td>
<td valign="top">4</td>
<td valign="top">5</td>
</tr>
</tbody>
</table>
<p>可惜现在使用的数据库是老旧的mysql 4.0，不支持子查询，不用子查询能实现这样的功能吗？我试了好多次都不能成功，比如：<br />select max(id), id, v, c from T group by c;<br />选出的结果为</p>
<table id="i5ef" cellspacing="0" cellpadding="3" width="300" bgcolor="#00cccc" border="1" unselectable="on">
<tbody>
<tr>
<td width="25%">max(id)</td>
<td width="25%">id</td>
<td width="25%">v</td>
<td width="25%">c</td>
</tr>
<tr>
<td width="25%">2</td>
<td width="25%">1</td>
<td width="25%">2</td>
<td width="25%">3</td>
</tr>
<tr>
<td width="25%">3</td>
<td width="25%">3</td>
<td width="25%">4</td>
<td width="25%">5</td>
</tr>
</tbody>
</table>
<p>v并非是max(id)对应的值.<br />如果加上having，如select max(id), v from T group by c having id = max(id); 则出现以下结果:</p>
<table id="p9d7" cellspacing="0" cellpadding="3" width="300" bgcolor="#00cccc" border="1" unselectable="on">
<tbody>
<tr>
<td width="25%">max(id)</td>
<td width="25%">id</td>
<td width="25%">v</td>
<td width="25%">c</td>
</tr>
<tr>
<td width="25%">3</td>
<td width="25%">3</td>
<td width="25%">4</td>
<td width="25%">5</td>
</tr>
</tbody>
</table>
<p>看样子having是对group by以后的数据起的效果。细想一下，又找到大学时的课本翻一下，仔细推敲觉得select是按这样的顺序进行的： <font color="#ff0000">原表-&gt;where-&gt;结果集-&gt;group by-&gt;分组后的结果集-&gt;聚合函数-&gt;带聚合结果的结果集（每个分组只取第一条)-&gt;having子句过滤-&gt;最终结果集。<font color="#330033">如果这结论是正确的话，那么只有在where时把不需要的去掉，或者在分组后能从分组里取id和max(id)一样的那条。如果用前者，我只能想到用子查询，如果用后者，好像走不通，没有办法控制分组内哪一条被选出。有没有更好的办法呀？</font></font></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.sulong.info/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.sulong.info/archives/24/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
