我有三张桌子:
用户(用户名,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的评论,我意识到我没有正确打印错误消息。
我收到错误消息:
错误:“ on子句”中的1054未知列“ businesses.address_id”
因此,现在我注意到“ address_id”列实际上在“ business”表中称为“ address”。