У меня есть три таблицы:
mysql> select * from purchase;
+-------------+-------------------------------------------+----------+--------+------------+
| purchase_id | list | quantity | sup_id | date |
+-------------+-------------------------------------------+----------+--------+------------+
| 1 | Hitachi Tv 75 so you can play Cod Warzone | 10 | 2 | 2018-12-20 |
| 2 | Hitachi Domotic System | 10 | 2 | 2019-01-15 |
| 3 | Mitsubishi Air Conditioner 12000 Btu | 5 | 1 | 2019-04-20 |
| 4 | Mitsubishi Air Conditioner 1000 Btu | 10 | 1 | 2019-04-25 |
| 5 | Tool Set Beta Professional | 20 | 4 | 2019-02-28 |
| 6 | Bridgestone Tyres 4x4 | 8 | 3 | 2019-01-31 |
| 7 | Mitsubishi Air Conditioner 3000 Btu | 10 | 1 | 2019-03-25 |
| 8 | Simple screw 10kg | 50 | 4 | 2019-01-30 |
| 9 | Bridgestone Tyres Motorbike 180/55 | 10 | 3 | 2019-03-21 |
| 10 | Bridgestone Tyres Motorbike 120/60 | 10 | 3 | 2019-03-21 |
| 11 | Hitachi Remote Control | 5 | 2 | 2019-06-01 |
| 12 | Tool Set Beta Professional | 5 | 4 | 2019-03-07 |
| 13 | Halfday peperonata from the balcony | 10 | 4 | 2019-01-03 |
+-------------+-------------------------------------------+----------+--------+------------+
mysql> select * from suppliers;
+--------+---------------------------+---------------------------------------------------+-----------+
| sup_id | name | address | phone |
+--------+---------------------------+---------------------------------------------------+-----------+
| 1 | Mitsubishi | 174-1000, Naka-ku, Yokohama | 291-1254 |
| 2 | Hitachi | 381-1234, Arakita, Katori-shi, Chiba | 381-1234 |
| 3 | Bridgestone | 238-1011, Otomo, Iwaizumi-cho Shimohei-gun, Iwate | 238-1011 |
| 4 | Il paradiso della Brugola | Via Milano | 02-564842 |
+--------+---------------------------+---------------------------------------------------+-----------+
mysql> select * from product;
+---------+-------------------------------------------+-------+
| prod_id | name | price |
+---------+-------------------------------------------+-------+
| 1 | Bridgestone Tyres Motorbike 180/55 | 150 |
| 2 | Bridgestone Tyres Motorbike 120/60 | 120 |
| 3 | Bridgestone Tyres 4x4 | 300 |
| 4 | Mitsubishi Air Conditioner 1000 Btu | 500 |
| 5 | Mitsubishi Air Conditioner 3000 Btu | 900 |
| 6 | Mitsubishi Air Conditioner 12000 Btu | 3000 |
| 7 | Hitachi Remote Control | 50 |
| 8 | Hitachi Domotic System | 400 |
| 9 | Hitachi Tv 75 so you can play Cod Warzone | 1500 |
| 10 | Tool Set Beta Professional | 100 |
| 11 | Simple screw 10kg | 10 |
| 12 | Halfday peperonata from the balcony | 5 |
+---------+-------------------------------------------+-------+
Я хотел бы отобразить для каждого поставщика общую сумму потраченных денег, и я сделал это:
mysql> SELECT purchase.sup_id,SUM(purchase.quantity*product.price) FROM product JOIN purchase ON product.name = purchase.list GROUP BY purchase.sup_id;
+--------+--------------------------------------+
| sup_id | SUM(purchase.quantity*product.price) |
+--------+--------------------------------------+
| 1 | 29000 |
| 2 | 19250 |
| 3 | 5100 |
| 4 | 3050 |
+--------+--------------------------------------+
Теперь я хотел бы отобразить название компании вместо sup_id.
Я пробовал это, но не работает:
SELECT purchase.sup_id,suppliers.name,SUM(purchase.quantity*product.price)
FROM product
JOIN purchase suppliers ON purchase.sup_id = suppliers.sup_id
GROUP BY purchase.sup_id;
Что я делаю не так?