我需要加入四个表并从中查询数据。我尝试了以下SQL查询来获取四个表中的数据
SELECT tbl_user.vFirst,tbl_user.vLast,tbl_feed.dCreatedDate,tbl_feed.tModifyDate,tbl_feed.fAddress, tbl_feed.tAddress,tbl_ratings.rate,tbl_ratings.comment,vCost
FROM tbl_feed
INNER JOIN tbl_user ON tbl_user.iUserID = tbl_feed.iUserID
INNER JOIN tbl_ratings ON tbl_ratings.trip_id = tbl_feed.iFeedID
INNER JOIN tbl_feed.iFeedID=tbl_nearest_drivers.iFeedID;
但是在运行上面的查询时。
错误1064(42000):您的SQL语法有错误;检查 手册,对应右边的MySQL服务器版本 在' .iFeedID'附近使用的语法在第1行
为什么这个JOIN不起作用?
答案 0 :(得分:4)
试试这个。
SELECT tbl_user.vFirst,tbl_user.vLast,tbl_feed.dCreatedDate,tbl_feed.tModifyDate,tbl_feed.fAddress, tbl_feed.tAddress,tbl_ratings.rate,tbl_ratings.comment,vCost
FROM tbl_feed
INNER JOIN tbl_user ON tbl_user.iUserID = tbl_feed.iUserID
INNER JOIN tbl_ratings ON tbl_ratings.trip_id = tbl_feed.iFeedID
INNER JOIN tbl_feed ON tbl_feed.iFeedID=tbl_nearest_drivers.iFeedID;
答案 1 :(得分:0)
希望此查询可以帮助您解决问题。在您的查询中,INNER JOIN
错过了表名tbl_nearest_drivers
。
SELECT
tbl_user.vFirst,
tbl_user.vLast,
tbl_feed.dCreatedDate,
tbl_feed.tModifyDate,
tbl_feed.fAddress,
tbl_feed.tAddress,
tbl_ratings.rate,
tbl_ratings.`comment`,
vCost
FROM
tbl_feed
INNER JOIN tbl_user ON tbl_user.iUserID = tbl_feed.iUserID
INNER JOIN tbl_ratings ON tbl_ratings.trip_id = tbl_feed.iFeedID
INNER JOIN tbl_nearest_drivers ON tbl_feed.iFeedID = tbl_nearest_drivers.iFeedID;
答案 2 :(得分:0)
您忘了在上次加入中说明表名:
SELECT tbl_user.vFirst,tbl_user.vLast,tbl_feed.dCreatedDate,tbl_feed.tModifyDate,tbl_feed.fAddress, tbl_feed.tAddress,tbl_ratings.rate,tbl_ratings.comment,vCost
FROM tbl_feed
INNER JOIN tbl_user ON tbl_user.iUserID = tbl_feed.iUserID
INNER JOIN tbl_ratings ON tbl_ratings.trip_id = tbl_feed.iFeedID
INNER JOIN tbl_nearest_drivers ON tbl_feed.iFeedID=tbl_nearest_drivers.iFeedID;
答案 3 :(得分:0)
SELECT tbl_user.vFirst,tbl_user.vLast,tbl_feed.dCreatedDate,tbl_feed.tModifyDate,tbl_feed.fAddress, tbl_feed.tAddress,tbl_ratings.rate,tbl_ratings.comment,vCost
FROM tbl_feed
INNER JOIN tbl_user ON tbl_user.iUserID = tbl_feed.iUserID
INNER JOIN tbl_ratings ON tbl_ratings.trip_id = tbl_feed.iFeedID
INNER JOIN tbl_nearest_drivers ON tbl_feed.iFeedID=tbl_nearest_drivers.iFeedID;
我想你觉得如此?