MySQL_常用查询的例子
3.4 拥有某个字段的组间最大值的行
“对每篇文章,找出有最贵的价格的交易者。”
在ANSI SQL中,我可以用这样一个子查询做到:
SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price)
FROM shop s2
WHERE s1.article = s2.article)
在MySQL中,最好是分几步做到:
得到一个表(文章,maxprice)。见3.4 拥有某个域的组间最大值的行。
对每篇文章,得到对应于存储最大价格的行。
这可以很容易用一个临时表做到:
CREATE TEMPORARY TABLE tmp (
article INT(4) UNSIGNED ZEROFILL DEFAULT ''0000'' NOT NULL,
price DOUBLE(16,2) DEFAULT ''0.00'' NOT NULL);
LOCK TABLES article read;
INSERT INTO tmp SELECT article, MAX(price) FROM shop GROUP BY article;
SELECT article, dealer, price FROM shop, tmp
WHERE shop.article=tmp.articel AND shop.price=tmp.price;
UNLOCK TABLES;
DROP TABLE tmp;
如果你不使用一个TEMPORARY表,你也必须锁定“tmp”表。
“它能一个单个查询做到吗?”
是的,但是只有使用我称之为“MAX-CONCAT诡计”的一个相当低效的诡计:
SELECT article,
SUBSTRING( MAX( CONCAT(LPAD(price,6,''0''),dealer) ), 7) AS dealer,
0.00+LEFT( MAX( CONCAT(LPAD(price,6,''0''),dealer) ), 6) AS price
FROM shop
GROUP BY article;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
最后例子当然能通过在客户程序中分割连结的列使它更有效一点。
3.5 使用外键
不需要外键联结2个表。
MySQL唯一不做的事情是CHECK以保证你使用的键确实在你正在引用表中存在,并且它不自动从有一个外键定义的表中删除行。如果你象平常那样使用你的键值,它将工作得很好!
CREATE TABLE persons (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirts (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM(''t-shirt'', ''polo'', ''dress'') NOT NULL,
color ENUM(''red'', ''blue'', ''orange'', ''white'', ''black'') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES persons,
PRIMARY KEY (id)
);
INSERT INTO persons VALUES (NULL, ''Antonio Paz'');
INSERT INTO shirts VALUES
(NULL, ''polo'', ''blue'', LAST_INSERT_ID()),
(NULL, ''dress'', ''white'', LAST_INSERT_ID()),
(NULL, ''t-shirt'', ''blue'', LAST_INSERT_ID());
INSERT INTO persons VALUES (NULL, ''Lilliana Angelovska'');
INSERT INTO shirts VALUES
(NULL, ''dress'', ''orange'', LAST_INSERT_ID()),
(NULL, ''polo'', ''red'', LAST_INSERT_ID()),
(NULL, ''dress'', ''blue'', LAST_INSERT_ID()),
(NULL, ''t-shirt'', ''white'', LAST_INSERT_ID());
SELECT * FROM persons;
+----+---------------------+
| id | name |
+----+---------------------+
| 1 | Antonio Paz |
| 2 | Lilliana Angelovska |
+----+---------------------+
SELECT * FROM shirts;
+----+---------+--------+-------+
| id | style | color | owner |
+----+---------+--------+-------+
| 1 | polo | blue | 1 |
| 2 | dress | white | 1 |
| 3 | t-shirt | blue | 1 |
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
| 7 | t-shirt | white | 2 |
+----+---------+--------+-------+
SELECT s.* FROM persons p, shirts s
WHERE p.name LIKE ''Lilliana%''
AND s.owner = p.id
AND s.color <> ''white'';
+----+-------+--------+-------+
| id | style | color | owner |
+----+-------+--------+-------+
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
+----+-------+--------+-------+
“对每篇文章,找出有最贵的价格的交易者。”
在ANSI SQL中,我可以用这样一个子查询做到:
SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price)
FROM shop s2
WHERE s1.article = s2.article)
在MySQL中,最好是分几步做到:
得到一个表(文章,maxprice)。见3.4 拥有某个域的组间最大值的行。
对每篇文章,得到对应于存储最大价格的行。
这可以很容易用一个临时表做到:
CREATE TEMPORARY TABLE tmp (
article INT(4) UNSIGNED ZEROFILL DEFAULT ''0000'' NOT NULL,
price DOUBLE(16,2) DEFAULT ''0.00'' NOT NULL);
LOCK TABLES article read;
INSERT INTO tmp SELECT article, MAX(price) FROM shop GROUP BY article;
SELECT article, dealer, price FROM shop, tmp
WHERE shop.article=tmp.articel AND shop.price=tmp.price;
UNLOCK TABLES;
DROP TABLE tmp;
如果你不使用一个TEMPORARY表,你也必须锁定“tmp”表。
“它能一个单个查询做到吗?”
是的,但是只有使用我称之为“MAX-CONCAT诡计”的一个相当低效的诡计:
SELECT article,
SUBSTRING( MAX( CONCAT(LPAD(price,6,''0''),dealer) ), 7) AS dealer,
0.00+LEFT( MAX( CONCAT(LPAD(price,6,''0''),dealer) ), 6) AS price
FROM shop
GROUP BY article;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
最后例子当然能通过在客户程序中分割连结的列使它更有效一点。
3.5 使用外键
不需要外键联结2个表。
MySQL唯一不做的事情是CHECK以保证你使用的键确实在你正在引用表中存在,并且它不自动从有一个外键定义的表中删除行。如果你象平常那样使用你的键值,它将工作得很好!
CREATE TABLE persons (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirts (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM(''t-shirt'', ''polo'', ''dress'') NOT NULL,
color ENUM(''red'', ''blue'', ''orange'', ''white'', ''black'') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES persons,
PRIMARY KEY (id)
);
INSERT INTO persons VALUES (NULL, ''Antonio Paz'');
INSERT INTO shirts VALUES
(NULL, ''polo'', ''blue'', LAST_INSERT_ID()),
(NULL, ''dress'', ''white'', LAST_INSERT_ID()),
(NULL, ''t-shirt'', ''blue'', LAST_INSERT_ID());
INSERT INTO persons VALUES (NULL, ''Lilliana Angelovska'');
INSERT INTO shirts VALUES
(NULL, ''dress'', ''orange'', LAST_INSERT_ID()),
(NULL, ''polo'', ''red'', LAST_INSERT_ID()),
(NULL, ''dress'', ''blue'', LAST_INSERT_ID()),
(NULL, ''t-shirt'', ''white'', LAST_INSERT_ID());
SELECT * FROM persons;
+----+---------------------+
| id | name |
+----+---------------------+
| 1 | Antonio Paz |
| 2 | Lilliana Angelovska |
+----+---------------------+
SELECT * FROM shirts;
+----+---------+--------+-------+
| id | style | color | owner |
+----+---------+--------+-------+
| 1 | polo | blue | 1 |
| 2 | dress | white | 1 |
| 3 | t-shirt | blue | 1 |
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
| 7 | t-shirt | white | 2 |
+----+---------+--------+-------+
SELECT s.* FROM persons p, shirts s
WHERE p.name LIKE ''Lilliana%''
AND s.owner = p.id
AND s.color <> ''white'';
+----+-------+--------+-------+
| id | style | color | owner |
+----+-------+--------+-------+
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
+----+-------+--------+-------+
0
相关文章