MySQL 查找价格最高的图书经销商的几种SQL语句
网络编程 2021-07-05 15:27www.168986.cn编程入门
不同的图书,在不同的经销商的价格不同,我们这里要找到每种图书最高的经销商是谁? 找最低的类似了。
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
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程