我的数据库结构如下图所示
我想选择所有分区,以及分区名称,城市名称和省名称。我的代码是这样的:
SELECT province.name as province, city.name as city, subdistrict.name as subdistrict
from provinces province, cities city, subdistricts subdistrict
WHERE province.id = city.id AND city.id = subdistrict.id;
但是我只得到了全部省份数据的总和,而不是所有分区数据的总和。
最佳答案
您的基本SQL缺少逗号,但通常应该可以使用。我强烈建议使用更现代的连接语法:
SELECT province.name AS province,
city.name AS city,
subdistrict.name AS subdistrict
FROM provinces province
INNER JOIN cities city
ON province.id = city.id
INNER JOIN subdistricts subdistrict
ON city.id = subdistrict.id;
但是,根据您的描述,我不确定您是否要进行一些汇总处理?
关于mysql - 在MySQL中连接3个表以选择字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41545317/