gpt4 book ai didi

MYSQL FROM多表多JOIN

转载 作者:行者123 更新时间:2023-11-30 22:51:10 27 4
gpt4 key购买 nike

我的 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 - “on 子句”中的未知列“feat.feature_id”

我的 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 列中的所有特征和所有燃料都在数据库中?

最佳答案

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

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

关于MYSQL FROM多表多JOIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28106054/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com