MySQL find the most expensive book dealers of several SQL statements _mysql
Source: Internet
Author: User
mysql> use test;
Database changed
Mysql> CREATE TABLE Shop (
-> article INT (4) UNSIGNED zerofill DEFAULT ' 0000 ' not NULL,
-> Dealer CHAR DEFAULT ' not NULL,
-> Price DOUBLE (16,2) DEFAULT ' 0.00 ' is 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 the 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 the 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
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.