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

xtfmy6hx  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(267)

我有4张tablepc、笔记本、智能手机和产品:

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 | SSD      |          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  |   both     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个表中选择一个最大价格-个人电脑,智能手机和笔记本电脑,这不是关于创建另一个表的最大值,其主要思想是显示使用命令只。我试图使用子查询,但似乎我有两只左手。

tyg4sfes

tyg4sfes1#

使用 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

相关问题