MySQL 查找價(jià)格最高的圖書(shū)經(jīng)銷商的幾種SQL語(yǔ)句
更新時(shí)間:2009年07月07日 01:55:20 作者:
不同的圖書(shū),在不同的經(jīng)銷商的價(jià)格不同,我們這里要找到每種圖書(shū)最高的經(jīng)銷商是誰(shuí)? 找最低的類似了。
mysql> use test;
Database changed
mysql> 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));
Query OK, 0 rows affected (0.13 sec)
mysql> 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);
Query OK, 7 rows affected (0.03 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> 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 |
+---------+--------+-------+
7 rows in set (0.06 sec)
mysql> select article,max(price) from shop group by article
-> ;
+---------+------------+
| article | max(price) |
+---------+------------+
| 0001 | 3.99 |
| 0002 | 10.99 |
| 0003 | 1.69 |
| 0004 | 19.95 |
+---------+------------+
4 rows in set (0.05 sec)
mysql> select article,max(price),dealer from shop group by article;
+---------+------------+--------+
| article | max(price) | dealer |
+---------+------------+--------+
| 0001 | 3.99 | A |
| 0002 | 10.99 | A |
| 0003 | 1.69 | B |
| 0004 | 19.95 | D |
+---------+------------+--------+
4 rows in set (0.00 sec)
mysql> select article,dealer,price from shop s1
-> where price=(select max(s2.price) from shop s2
-> where s1.article=s2.article);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
4 rows in set (0.01 sec)
mysql> select s1.article,dealer,s1.price
-> from shop s1
-> join(
-> select article,max(price) as price from shop
-> group by article) as s2
-> on s1.article = s2.article and s1.price = s2.price;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
4 rows in set (0.05 sec)
mysql> select s1.article,s1.dealer,s1.price from shop s1
-> left join shop s2 on s1.article=s2.article and s1.price select s1.article,s1.dealer,s1.price,s2.* from shop s1 left join shop s2
on s1.article=s2.article and s1.price
Database changed
mysql> 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));
Query OK, 0 rows affected (0.13 sec)
mysql> 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);
Query OK, 7 rows affected (0.03 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> 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 |
+---------+--------+-------+
7 rows in set (0.06 sec)
mysql> select article,max(price) from shop group by article
-> ;
+---------+------------+
| article | max(price) |
+---------+------------+
| 0001 | 3.99 |
| 0002 | 10.99 |
| 0003 | 1.69 |
| 0004 | 19.95 |
+---------+------------+
4 rows in set (0.05 sec)
mysql> select article,max(price),dealer from shop group by article;
+---------+------------+--------+
| article | max(price) | dealer |
+---------+------------+--------+
| 0001 | 3.99 | A |
| 0002 | 10.99 | A |
| 0003 | 1.69 | B |
| 0004 | 19.95 | D |
+---------+------------+--------+
4 rows in set (0.00 sec)
mysql> select article,dealer,price from shop s1
-> where price=(select max(s2.price) from shop s2
-> where s1.article=s2.article);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
4 rows in set (0.01 sec)
mysql> select s1.article,dealer,s1.price
-> from shop s1
-> join(
-> select article,max(price) as price from shop
-> group by article) as s2
-> on s1.article = s2.article and s1.price = s2.price;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
4 rows in set (0.05 sec)
mysql> select s1.article,s1.dealer,s1.price from shop s1
-> left join shop s2 on s1.article=s2.article and s1.price select s1.article,s1.dealer,s1.price,s2.* from shop s1 left join shop s2
on s1.article=s2.article and s1.price
您可能感興趣的文章:
- PHP+MySQL使用mysql_num_rows實(shí)現(xiàn)模糊查詢圖書(shū)信息功能
- php基于dom實(shí)現(xiàn)讀取圖書(shū)xml格式數(shù)據(jù)的方法
- php基于dom實(shí)現(xiàn)的圖書(shū)xml格式數(shù)據(jù)示例
- php根據(jù)isbn書(shū)號(hào)查詢amazon網(wǎng)站上的圖書(shū)信息的示例
- 簡(jiǎn)單的php購(gòu)物車代碼
- php實(shí)現(xiàn)簡(jiǎn)單加入購(gòu)物車功能
- PHP實(shí)現(xiàn)的購(gòu)物車類實(shí)例
- 基于PHP+Mysql簡(jiǎn)單實(shí)現(xiàn)了圖書(shū)購(gòu)物車系統(tǒng)的實(shí)例詳解
相關(guān)文章
MySQL筆記之?dāng)?shù)據(jù)備份與還原的使用詳解
數(shù)據(jù)很重要,這點(diǎn)用腳趾頭想都知道,為了保證數(shù)據(jù)的安全,因此需要定期對(duì)數(shù)據(jù)備份2013-05-05
CentOS 7搭建多實(shí)例MySQL8的詳細(xì)教程(想要幾個(gè)搞幾個(gè))
這篇文章主要介紹了CentOS 7搭建多實(shí)例MySQL8的詳細(xì)教程(想要幾個(gè)搞幾個(gè)),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
MySQL設(shè)置用戶權(quán)限的簡(jiǎn)單步驟
這篇文章主要給大家介紹了關(guān)于MySQL設(shè)置用戶權(quán)限的簡(jiǎn)單步驟,學(xué)習(xí)MySQL數(shù)據(jù)庫(kù),MySQL用戶權(quán)限設(shè)置是需要首先學(xué)習(xí)的,需要的朋友可以參考下2023-07-07
MySQL所支持的數(shù)據(jù)類型與表字段約束類型的學(xué)習(xí)教程
這篇文章主要介紹了MySQL所支持的數(shù)據(jù)類型與表字段約束類型的學(xué)習(xí)教程,是MySQL入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-12-12
MySQL批量修改表及表內(nèi)字段排序規(guī)則舉例詳解
在MySQL中字段排序規(guī)則(也稱為字符集和排序規(guī)則)用于確定如何比較和排序字符串,下面這篇文章主要給大家介紹了關(guān)于MySQL批量修改表及表內(nèi)字段排序規(guī)則的相關(guān)資料,需要的朋友可以參考下2024-05-05
myeclipse中連接mysql數(shù)據(jù)庫(kù)示例代碼
這篇文章主要為大家詳細(xì)介紹了MyEclipse連接MySQL數(shù)據(jù)庫(kù)圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Mysql數(shù)據(jù)庫(kù)的導(dǎo)入導(dǎo)出方式(各種情況)
這篇文章主要介紹了Mysql數(shù)據(jù)庫(kù)的導(dǎo)入導(dǎo)出方式(各種情況),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

