我有以下的數據庫結構:如何選擇多個表中的值?
car
+----------+------------+----------------+-----------------+
| car_id | car_name | car_model_id | car_factory_id |
+----------+------------+----------------+-----------------+
car_model
+---------------+----------------+
| car_model_id | car_model_name |
+---------------+----------------+
factory
+-------------+---------------+-------------------+
| factory_id | factory_name | factory_location |
+-------------+---------------+-------------------+
factory_location
+----------------------+------------------------+
| factory_location_id | factory_location_name |
+----------------------+------------------------+
我想:
- 列表中的所有工廠
- 計數在每家工廠
- 通過分組生產的汽車數量car_model
輸出應該是這樣的:
| factory_id | car_model_id = 1 | car_model_id = 2 | car_model_id = ... |
+------------+------------------+------------------+--------------------+
| 1 | 145 | 3307 | ... |
+------------+------------------+------------------+--------------------+
| 2 | 32798 | 345 | ... |
+------------+------------------+------------------+--------------------+
| 3 | 0 | 10 | ... |
+------------+------------------+------------------+--------------------+
| ... | ... | ... | ... |
+------------+------------------+------------------+--------------------+
我最親密的嘗試是這樣的要求,但它仍然不是我想要的東西:
SELECT COUNT(car_id) FROM car GROUP BY car_factory_id AND car_model_id
我的RDBMS是MySQL 5.1中。
感謝所有幫助:)
這是什麼RDBMS? –
^這個,看看內部連接。 – zimdanen
RDBMS是mySQL 5.1 –