MYSQL:如何根据另一个表中的数据选择列?

时间:2019-09-13 20:37:10

标签: mysql sql database

我有三个表:

技术人员-包含技术名称数据以及他们从中获取股票的股票提供者

库存订单-他们订购的物品列表

股票代码-物品名称的列表以及取决于提供者的不同代码。

我正在尝试执行一个查询,该查询根据以下结果表根据技术人员从哪个供应商获取其库存的信息来选择相关的产品代码

有人可以帮我吗?

提前谢谢。

表格

技术员

+-------------+----------------+
| Technician  | Stock Supplier |
+-------------+----------------+
| Bob Jones   | Provider 1     |
| Erika Jones | Provider 1     |
| Steve Jones | Provider 2     |
+-------------+----------------+

股票订单


+-------------+-----------------------+-------------------+
|  Tech Name  |     Product Name      |     Category      |
+-------------+-----------------------+-------------------+
| Bob Jones   | WURTH PASTE GREY      | CONSUMABLES       |
| Bob Jones   | RADIANT WAX           | VALETING PRODUCTS |
| Bob Jones   | TELESCOPIC MAGNET     | HARDWARE          |
| Steve Jones | TELESCOPIC MAGNET     | HARDWARE          |
| Steve Jones | SAFETY GLASSES (PREP) | HARDWARE          |
| Erika Jones | ENGINE OIL 1L         | MAINTENANCE ITEMS |
| Erika Jones | WURTH VAKU 50 FILLER  | CONSUMABLES       |
+-------------+-----------------------+-------------------+

股票代码




+-------------------+-----------------------------------+-------------------------+-------------------------+----------+-------+
|     CATEGORY      |               ITEM                | PROVIDER 1 PRODUCT CODE | PROVIDER 2 PRODUCT CODE | QUANTITY | PRICE |
+-------------------+-----------------------------------+-------------------------+-------------------------+----------+-------+
| VALETING PRODUCTS | RADIANT WAX                       | CON.46105               | 46101                   |        1 |  4.92 |
| CONSUMABLES       | WURTH PASTE BLACK                 | 8932801                 | 8932801                 |        1 | 21.44 |
| CONSUMABLES       | WURTH PASTE GREY                  | 8932802                 | 8932802                 |        1 | 21.44 |
| CONSUMABLES       | WURTH PLASTIC CLEANER             | 8935001                 | 8935001                 |        1 | 12.99 |
| CONSUMABLES       | WURTH VAKU 50 FILLER              | 89260501                | 89260501                |        1 | 14.95 |
| MAINTENANCE ITEMS | ENGINE OIL 1L                     | 521772392               | 521772392               |        1 |  5.63 |
| MAINTENANCE ITEMS | RED ANTIFREEZE (65 PLATE ONWARDS) | 523770482               | 523770482               |        1 |   2.3 |
| HARDWARE          | TELESCOPIC MAGNET                 | 553777300               | 60370                   |        1 |  2.32 |
| HARDWARE          | SAFETY GLASSES (PREP)             | 3M2720S                 | 15SC                    |        1 |  2.83 |
| HARDWARE          | SAFETY GLASSES (OVER SPECTACLES)  | 3M2800                  | 15SC                    |        1 |  3.06 |
| PPE               | SIZE 9 MECHANIC STYLE GLOVES      | GRE.30305509            | 303-MAT-PR              |        1 |  1.48 |
+-------------------+-----------------------------------+-------------------------+-------------------------+----------+-------+



结果



+-------------+-----------------------+-------------------+-----------+
|  Tech Name  |         Name          |     Category      |   Code    |
+-------------+-----------------------+-------------------+-----------+
| Bob Jones   | WURTH PASTE GREY      | CONSUMABLES       | 8932802   |
| Bob Jones   | RADIANT WAX           | VALETING PRODUCTS | CON.46105 |
| Bob Jones   | TELESCOPIC MAGNET     | HARDWARE          | 553777300 |
| Steve Jones | TELESCOPIC MAGNET     | HARDWARE          | 60370     |
| Steve Jones | SAFETY GLASSES (PREP) | HARDWARE          | 15SC      |
| Erika Jones | ENGINE OIL 1L         | MAINTENANCE ITEMS | 521772392 |
| Erika Jones | WURTH VAKU 50 FILLER  | CONSUMABLES       | 89260501  |
+-------------+-----------------------+-------------------+-----------+

1 个答案:

答案 0 :(得分:1)

使用CASE WHEN根据code表中stock_supplier的值对technician进行条件提取。

SELECT 
    so.tech_name 'Tech Name',
    so.product_name Name,
    so.category Category,
    CASE t.stock_supplier
        WHEN 'Provider 1' THEN sc.PROVIDER_1_PRODUCT_CODE
        WHEN 'Provider 2' THEN sc.PROVIDER_2_PRODUCT_CODE
    END AS Code
FROM stock_order so
INNER JOIN technician t ON so.tech_name = t.technician
INNER JOIN stock_codes sc ON so.product_name = sc.item;