gpt4 book ai didi

php - 连接 3 个表并使用 WHERE

转载 作者:行者123 更新时间:2023-11-29 21:09:28 25 4
gpt4 key购买 nike

我有三个表:-

用户(用户名、business_id 等)

企业(business_id、service_type、纬度、经度等)

地址(address_id、country_iso 等)

我有以下查询,效果很好:

SELECT username, users.business_id, businesses.latitude, businesses.longitude
FROM users
JOIN businesses
ON users.business_id = businesses.business_id
WHERE service_type = ?

现在,我还想按“country_iso”值(“地址”表中的一列)过滤结果。

我尝试执行以下操作,但收到错误:

SELECT username, users.business_id, businesses.latitude, businesses.longitude 
FROM users
JOIN businesses
ON users.business_id = businesses.business_id
JOIN addresses
ON businesses.address_id = addresses.address_id
WHERE businesses.service_type = ?
AND addresses.country_iso = ?

我也尝试了以下方法,但仍然出现错误:

SELECT username, users.business_id, businesses.latitude, businesses.longitude 
FROM users
JOIN businesses
ON users.business_id = businesses.business_id
AND businesses.service_type = ?
JOIN addresses
ON businesses.address_id = addresses.address_id
WHERE addresses.country_iso = ?

获得我需要的结果的正确方法是什么?

最佳答案

感谢 Juergen d 的评论,我意识到我没有正确打印错误消息。我收到错误消息:错误:1054“on 子句”中的未知列“businesses.address_id”

现在我注意到“address_id”列实际上在“business”表中称为“address”。

关于php - 连接 3 个表并使用 WHERE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36515611/

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