下面是一些學習如何用MySQL解決一些常見問題的例子。一些例子使用數據庫表“shop”,包含某個商人的每篇文章(物品號)的價格。假定每個商人的每篇文章有一個單獨的固定價格,那麼(物品,商人)是記錄的主鍵。
你能這樣創建例子數據庫表:
CREATE TABLE shop (
article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,
dealer CHAR(20) DEFAULT '' NOT NULL,
price DOUBLE(16,2) DEFAULT '0.00' NOT NULL,
PRIMARY KEY(article, dealer));
INSERT INTO shop VALUES
(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),(3,'C',1.69),
(3,'D',1.25),(4,'D',19.95);
好了,例子數據是這樣的:
SELECT * FROM shop
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|0001 | A | 3.45 |
|0001 | B | 3.99 |
|0002 | A | 10.99 |
|0003 | B | 1.45 |
|0003 | C | 1.69 |
|0003 | D | 1.25 |
|0004 | D | 19.95 |
+---------+--------+-------+
3.1 列的最大值
“最大的物品號是什麼?”
SELECT MAX(article) AS article FROM shop
+---------+
| article |
+---------+
| 4 |
+---------+
3.2 擁有某個列的最大值的行
“找出最貴的文章的編號、商人和價格”
在ANSI-SQL中這很容易用一個子查詢做到:
SELECT article, dealer, price
FROM shop
WHERE price=(SELECT MAX(price) FROM shop)
在MySQL中(還沒有子查詢)就用2步做到:
用一個SELECT語句從表中得到最大值。
使用該值編出實際的查詢:
SELECT article, dealer, price
FROM shop
WHERE price=19.95
另一個解決方案是按價格降序排序所有行並用MySQL特定LIMIT子句只得到的第一行:
SELECT article, dealer, price
FROM shop
ORDER BY price DESC
LIMIT 1
注意:如果有多個最貴的文章( 例如每個19.95),LIMIT解決方案僅僅顯示他們之一!
3.3 列的最大值:按組:只有值
“每篇文章的最高的價格是什麼?”
SELECT article, MAX(price) AS price
FROM shop
GROUP BY article
+---------+-------+
| article | price |
+---------+-------+
|0001 | 3.99 |
|0002 | 10.99 |
|0003 | 1.69 |
|0004 | 19.95 |
+---------+-------+
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 |
+----+-------+--------+-------+