MySQL - 如何从多个表中选择最大值(只有一个值)

时间:2018-05-18 19:55:10

标签: mysql sql unix terminal

我有4张桌子PC,笔记本电脑,智能手机和产品:

    select * from Notebook;
+------------+----------+-----------+----------+-------------+----------+-------+
| model_name | cpu_core | cpu_speed | hdd_type | screen_size | hdd_size | price |
+------------+----------+-----------+----------+-------------+----------+-------+
| GP62M      |        6 |      3500 | SSD      |          17 |      256 |  2300 |
| GE62       |        4 |      3300 | SSD      |          15 |      128 |  2100 |
| GL62       |        4 |      3000 | SSD      |          15 |      128 |  1900 |
| Vostro     |        4 |      2200 | HDD      |          17 |      500 |  1800 |
| Inspiron   |        4 |      1600 | HDD      |          15 |      500 |  1600 |
| Air        |        4 |      1800 | SDD      |          13 |      512 |  3000 |
| Swift 5    |        4 |      4000 | SSD      |          14 |      512 |  2750 |
| Swift 6    |        6 |      4000 | SSD      |          17 |      512 |  2950 |
+------------+----------+-----------+----------+-------------+----------+-------+

select * from PC;
+------------+----------+-----------+------+----------+-------+
| model_name | cpu_core | cpu_speed | ram  | hdd_size | price |
+------------+----------+-----------+------+----------+-------+
| XPS        |        6 |      2200 |   16 |     1000 |  2100 |
| Surface    |        4 |      2000 |   32 |     2000 |  2400 |
| Magnus     |        4 |      2000 |    6 |     2000 |  1800 |
| Aurora A6  |        4 |      2200 |   32 |     2200 |  2300 |
| Trudent 3  |        4 |      2400 |   16 |     2400 |  2000 |
+------------+----------+-----------+------+----------+-------+




   select * from Product;
+-----------+---------------+------------+
| vendor    | model_name    | type       |
+-----------+---------------+------------+
| DELL      | XPS           | PC         |
| DELL      | Trudent 3     | PC         |
| DELL      | Aurora A6     | PC         |
| NoteShop  | Surface       | PC         |
| NoteShop  | Magnus        | PC         |
| LapShop   | GP62M         | Notebook   |
| LapShop   | GE62          | Notebook   |
| LapShop   | GL62          | Notebook   |
| LapShop   | Vostro        | Notebook   |
| LapShop   | Inspiron      | Notebook   |
| AppleShop | Air           | Notebook   |
| AppleShop | 3G            | Smartphone |
| AppleShop | 4G            | Smartphone |
| AppleShop | 5             | Smartphone |
| AppleShop | 5G            | Smartphone |
| Xiaomi    | Redmi note 3  | Smartphone |
| Xiaomi    | Redmi note 4  | Smartphone |
| Xiaomi    | Redmi note 5  | Smartphone |
| Samsung   | Galaxy note 5 | Smartphone |
| Samsung   | Galaxy note 8 | Smartphone |
| NoteShop  | Swift 5       | Notebook   |
| NoteShop  | Swift 6       | Notebook   |
+-----------+---------------+------------+

select * from Smartphone;
+---------------+----------+-----------+------------+---------+------+-------+
| model_name    | cpu_core | cpu_speed | flash_size | OS      | lte  | price |
+---------------+----------+-----------+------------+---------+------+-------+
| 3G            |        2 |      1200 |          8 | Ios     | -    |   450 |
| 4G            |        3 |      1600 |          8 | Ios     | +    |   850 |
| 5             |        3 |      1800 |         16 | Ios     | +    |  1250 |
| 5G            |        4 |      2000 |          8 | Ios     | +    |  1350 |
| Redmi note 3  |        3 |      2000 |          8 | Android | +    |   800 |
| Redmi note 4  |        4 |      2200 |         16 | Android | +    |  1200 |
| Redmi note 5  |        6 |      2400 |         16 | Android | +    |  1300 |
| Galaxy note 5 |        6 |      2200 |          8 | Android | +    |  1100 |
| Galaxy note 8 |        8 |      2400 |          8 | Android | +    |  1400 |
+---------------+----------+-----------+------------+---------+------+-------+

我需要从3个表中选择一个最高价格 - PC,智能手机和笔记本,而不是创建另一个具有最大值的表,主要思想是仅显示使用命令。 我试图使用子查询,但似乎我有两个左手) 希望你能帮助我,并提前多多感谢你!

1 个答案:

答案 0 :(得分:0)

在子查询中使用UNION

SELECT MAX(price) AS maxPrice
FROM (
    SELECT MAX(price) AS price FROM PC
    UNION
    SELECT MAX(price) AS price FROM Smartphone
    UNION
    SELECT MAX(price) AS price FROM Notebook
) AS alltables