MYSQL FROM多个表和多个JOIN

时间:2015-01-23 08:46:55

标签: mysql join

我的SQL代码:

SELECT
    *
FROM
    map_features_stations,
    map_fuels_stations
RIGHT JOIN fuelprices ON fuelprices.fuel_id = map_fuels_stations.fuel_id
RIGHT JOIN map_stations ON map_fuels_stations.station_id = map_stations.id
RIGHT JOIN map_locations ON map_stations.location_id = map_locations.id
RIGHT JOIN map_features ON map_features.id = map_features_stations.feature_id

我有错误[Err] 1054 - Unknown column 'feat.feature_id' in 'on clause'

我在map_features_stations表中有这个列。

SELECT * FROM map_features_stations
INNER JOIN map_features ON map_features.id = map_features_stations.feature_id

这很好......

有人可以帮助我吗?


已更新


SELECT
    GROUP_CONCAT(DISTINCT CONCAT(map_features.id, '-', map_features.name_eng)) AS 'existingFeatures',
    GROUP_CONCAT(DISTINCT map_features_stations.feature_id) AS 'allFeatures',
    GROUP_CONCAT(DISTINCT map_fuels_stations.fuel_id) AS 'existingFuels',
    GROUP_CONCAT(DISTINCT CONCAT(fuelprices.fueL_id, '-', fuelprices.fuel_name)) AS 'allFeatures'

    FROM map_fuels_stations
    LEFT JOIN fuelprices ON fuelprices.fuel_id = map_fuels_stations.fuel_id
    LEFT JOIN map_stations ON map_fuels_stations.station_id = map_stations.id

    RIGHT JOIN map_locations ON map_stations.location_id = map_locations.id

    RIGHT JOIN map_features_stations ON map_features_stations.station_id = map_stations.id
    RIGHT JOIN map_features ON map_features_stations.feature_id = map_features.id

    GROUP BY map_stations.id
    HAVING map_stations.id = 1

我改变了它。结果是: enter image description here

在现有(existingGeatures和existingFuels)列中,一切都很好。但是我如何才能在allFeatures和allFuels列中拥有数据库中的所有功能和所有燃料?

1 个答案:

答案 0 :(得分:0)

不要混合隐式和显式连接语法。在查询中的任何位置使用显式连接

SELECT
    *
FROM
    map_features_stations
JOIN map_fuels_stations on ...
RIGHT JOIN fuelprices ON fuelprices.fuel_id = map_fuels_stations.fuel_id
RIGHT JOIN map_stations ON map_fuels_stations.station_id = map_stations.id
RIGHT JOIN map_locations ON map_stations.location_id = map_locations.id
RIGHT JOIN map_features ON map_features.id = map_features_stations.feature_id