<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[mysql常用查询的例子]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[数据库技术]]></category>
<pubDate>Mon, 04 Sep 2006 00:19:37 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	下面是一些学习如何用MySQL解决一些常见问题的例子。<br/><br/>一些例子使用数据库表“shop”，包含某个商人的每篇文章(物品号)的价格。假定每个商人的每篇文章有一个单独的固定价格，那么(物品，商人)是记录的主键。 <br/><br/>你能这样创建例子数据库表： <br/><br/>CREATE TABLE shop (<br/> article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,<br/> dealer &nbsp;CHAR(20) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DEFAULT '' &nbsp; &nbsp; NOT NULL,<br/> price &nbsp; DOUBLE(16,2) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DEFAULT '0.00' NOT NULL,<br/> PRIMARY KEY(article, dealer));<br/><br/>INSERT INTO shop VALUES<br/>(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),(3,'C',1.69),<br/>(3,'D',1.25),(4,'D',19.95);<br/><br/>好了，例子数据是这样的： <br/><br/>SELECT * FROM shop<br/><br/>+---------+--------+-------+<br/>&#124; article &#124; dealer &#124; price &#124;<br/>+---------+--------+-------+<br/>&#124; &nbsp; &nbsp;0001 &#124; A &nbsp; &nbsp; &nbsp;&#124; &nbsp;3.45 &#124;<br/>&#124; &nbsp; &nbsp;0001 &#124; B &nbsp; &nbsp; &nbsp;&#124; &nbsp;3.99 &#124;<br/>&#124; &nbsp; &nbsp;0002 &#124; A &nbsp; &nbsp; &nbsp;&#124; 10.99 &#124;<br/>&#124; &nbsp; &nbsp;0003 &#124; B &nbsp; &nbsp; &nbsp;&#124; &nbsp;1.45 &#124;<br/>&#124; &nbsp; &nbsp;0003 &#124; C &nbsp; &nbsp; &nbsp;&#124; &nbsp;1.69 &#124;<br/>&#124; &nbsp; &nbsp;0003 &#124; D &nbsp; &nbsp; &nbsp;&#124; &nbsp;1.25 &#124;<br/>&#124; &nbsp; &nbsp;0004 &#124; D &nbsp; &nbsp; &nbsp;&#124; 19.95 &#124;<br/>+---------+--------+-------+<br/><br/>8.3.1 列的最大值<br/>“最大的物品号是什么？” <br/><br/>SELECT MAX(article) AS article FROM shop<br/><br/>+---------+<br/>&#124; article &#124;<br/>+---------+<br/>&#124; &nbsp; &nbsp; &nbsp; 4 &#124;<br/>+---------+<br/><br/>8.3.2 拥有某个列的最大值的行<br/>“找出最贵的文章的编号、商人和价格”<br/><br/>在ANSI-SQL中这很容易用一个子查询做到： <br/><br/>SELECT article, dealer, price<br/>FROM &nbsp; shop<br/>WHERE &nbsp;price=(SELECT MAX(price) FROM shop)<br/><br/>在MySQL中（还没有子查询)就用2步做到： <br/><br/>用一个SELECT语句从表中得到最大值。 <br/>使用该值编出实际的查询： <br/>SELECT article, dealer, price<br/>FROM &nbsp; shop<br/>WHERE &nbsp;price=19.95<br/><br/>另一个解决方案是按价格降序排序所有行并用MySQL特定LIMIT子句只得到的第一行： <br/><br/>SELECT article, dealer, price<br/>FROM &nbsp; shop<br/>ORDER BY price DESC<br/>LIMIT 1<br/><br/>注意:如果有多个最贵的文章( 例如每个19.95)，LIMIT解决方案仅仅显示他们之一！ <br/><br/>8.3.3 列的最大值：按组：只有值<br/>“每篇文章的最高的价格是什么？” <br/><br/>SELECT article, MAX(price) AS price<br/>FROM &nbsp; shop<br/>GROUP BY article<br/><br/>+---------+-------+<br/>&#124; article &#124; price &#124;<br/>+---------+-------+<br/>&#124; &nbsp; &nbsp;0001 &#124; &nbsp;3.99 &#124;<br/>&#124; &nbsp; &nbsp;0002 &#124; 10.99 &#124;<br/>&#124; &nbsp; &nbsp;0003 &#124; &nbsp;1.69 &#124;<br/>&#124; &nbsp; &nbsp;0004 &#124; 19.95 &#124;<br/>+---------+-------+<br/><br/>8.3.4 拥有某个字段的组间最大值的行<br/>“对每篇文章，找出有最贵的价格的交易者。” <br/><br/>在ANSI SQL中，我可以用这样一个子查询做到： <br/><br/>SELECT article, dealer, price<br/>FROM &nbsp; shop s1<br/>WHERE &nbsp;price=(SELECT MAX(s2.price)<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM shop s2<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WHERE s1.article = s2.article)<br/><br/>在MySQL中，最好是分几步做到： <br/><br/>得到一个表(文章，maxprice)。见8.3.4 拥有某个域的组间最大值的行。 <br/>对每篇文章，得到对应于存储最大价格的行。 <br/>这可以很容易用一个临时表做到： <br/><br/>CREATE TEMPORARY TABLE tmp (<br/> &nbsp; &nbsp; &nbsp; &nbsp;article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,<br/> &nbsp; &nbsp; &nbsp; &nbsp;price &nbsp; DOUBLE(16,2) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DEFAULT '0.00' NOT NULL);<br/><br/>LOCK TABLES article read;<br/><br/>INSERT INTO tmp SELECT article, MAX(price) FROM shop GROUP BY article;<br/><br/>SELECT article, dealer, price FROM shop, tmp<br/>WHERE shop.article=tmp.articel AND shop.price=tmp.price;<br/><br/>UNLOCK TABLES;<br/><br/>DROP TABLE tmp;<br/><br/>如果你不使用一个TEMPORARY表，你也必须锁定“tmp”表。 <br/><br/>“它能一个单个查询做到吗？” <br/><br/>是的，但是只有使用我称之为“MAX-CONCAT诡计”的一个相当低效的诡计： <br/><br/>SELECT article,<br/> &nbsp; &nbsp; &nbsp; SUBSTRING( MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 7) AS dealer,<br/> &nbsp;0.00+LEFT( &nbsp; &nbsp; &nbsp;MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 6) AS price<br/>FROM &nbsp; shop<br/>GROUP BY article;<br/><br/>+---------+--------+-------+<br/>&#124; article &#124; dealer &#124; price &#124;<br/>+---------+--------+-------+<br/>&#124; &nbsp; &nbsp;0001 &#124; B &nbsp; &nbsp; &nbsp;&#124; &nbsp;3.99 &#124;<br/>&#124; &nbsp; &nbsp;0002 &#124; A &nbsp; &nbsp; &nbsp;&#124; 10.99 &#124;<br/>&#124; &nbsp; &nbsp;0003 &#124; C &nbsp; &nbsp; &nbsp;&#124; &nbsp;1.69 &#124;<br/>&#124; &nbsp; &nbsp;0004 &#124; D &nbsp; &nbsp; &nbsp;&#124; 19.95 &#124;<br/>+---------+--------+-------+<br/><br/>最后例子当然能通过在客户程序中分割连结的列使它更有效一点。 <br/><br/>8.3.5 使用外键<br/>不需要外键联结2个表。 <br/><br/>MySQL唯一不做的事情是CHECK以保证你使用的键确实在你正在引用表中存在，并且它不自动从有一个外键定义的表中删除行。如果你象平常那样使用你的键值，它将工作得很好！ <br/><br/>CREATE TABLE persons (<br/> &nbsp; &nbsp;id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, <br/> &nbsp; &nbsp;name CHAR(60) NOT NULL,<br/> &nbsp; &nbsp;PRIMARY KEY (id)<br/>);<br/><br/>CREATE TABLE shirts (<br/> &nbsp; &nbsp;id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,<br/> &nbsp; &nbsp;style ENUM('t-shirt', 'polo', 'dress') NOT NULL,<br/> &nbsp; &nbsp;color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,<br/> &nbsp; &nbsp;owner SMALLINT UNSIGNED NOT NULL REFERENCES persons,<br/> &nbsp; &nbsp;PRIMARY KEY (id)<br/>);<br/><br/>INSERT INTO persons VALUES (NULL, 'Antonio Paz');<br/><br/>INSERT INTO shirts VALUES<br/>(NULL, 'polo', 'blue', LAST_INSERT_ID()),<br/>(NULL, 'dress', 'white', LAST_INSERT_ID()),<br/>(NULL, 't-shirt', 'blue', LAST_INSERT_ID());<br/><br/>INSERT INTO persons VALUES (NULL, 'Lilliana Angelovska');<br/><br/>INSERT INTO shirts VALUES<br/>(NULL, 'dress', 'orange', LAST_INSERT_ID()),<br/>(NULL, 'polo', 'red', LAST_INSERT_ID()),<br/>(NULL, 'dress', 'blue', LAST_INSERT_ID()),<br/>(NULL, 't-shirt', 'white', LAST_INSERT_ID());<br/><br/>SELECT * FROM persons;<br/>+----+---------------------+<br/>&#124; id &#124; name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#124;<br/>+----+---------------------+<br/>&#124; &nbsp;1 &#124; Antonio Paz &nbsp; &nbsp; &nbsp; &nbsp; &#124;<br/>&#124; &nbsp;2 &#124; Lilliana Angelovska &#124;<br/>+----+---------------------+<br/><br/>SELECT * FROM shirts;<br/>+----+---------+--------+-------+<br/>&#124; id &#124; style &nbsp; &#124; color &nbsp;&#124; owner &#124;<br/>+----+---------+--------+-------+<br/>&#124; &nbsp;1 &#124; polo &nbsp; &nbsp;&#124; blue &nbsp; &#124; &nbsp; &nbsp; 1 &#124;<br/>&#124; &nbsp;2 &#124; dress &nbsp; &#124; white &nbsp;&#124; &nbsp; &nbsp; 1 &#124;<br/>&#124; &nbsp;3 &#124; t-shirt &#124; blue &nbsp; &#124; &nbsp; &nbsp; 1 &#124;<br/>&#124; &nbsp;4 &#124; dress &nbsp; &#124; orange &#124; &nbsp; &nbsp; 2 &#124;<br/>&#124; &nbsp;5 &#124; polo &nbsp; &nbsp;&#124; red &nbsp; &nbsp;&#124; &nbsp; &nbsp; 2 &#124;<br/>&#124; &nbsp;6 &#124; dress &nbsp; &#124; blue &nbsp; &#124; &nbsp; &nbsp; 2 &#124;<br/>&#124; &nbsp;7 &#124; t-shirt &#124; white &nbsp;&#124; &nbsp; &nbsp; 2 &#124;<br/>+----+---------+--------+-------+<br/><br/>SELECT s.* FROM persons p, shirts s<br/> WHERE p.name LIKE 'Lilliana%'<br/> &nbsp; AND s.owner = p.id<br/> &nbsp; AND s.color <> 'white';<br/><br/>+----+-------+--------+-------+<br/>&#124; id &#124; style &#124; color &nbsp;&#124; owner &#124;<br/>+----+-------+--------+-------+<br/>&#124; &nbsp;4 &#124; dress &#124; orange &#124; &nbsp; &nbsp; 2 &#124;<br/>&#124; &nbsp;5 &#124; polo &nbsp;&#124; red &nbsp; &nbsp;&#124; &nbsp; &nbsp; 2 &#124;<br/>&#124; &nbsp;6 &#124; dress &#124; blue &nbsp; &#124; &nbsp; &nbsp; 2 &#124;<br/>+----+-------+--------+-------+<br/>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] mysql常用查询的例子]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>