我正在尝试获取两个表的left outer join
个2个表,但我无法执行此查询,phpmyadmin在运行此查询时提供#1064 error on line 12
:
SELECT
pt.id as planid,
pt.trip_name,
pt.description,
cor.latitude,
cor.longitude,
bb.id as bookmarkid,
bb.num_of_persons as persons
FROM
planned_trips as pt,
coordinates as cor,
LEFT JOIN Bookmarkedby as bb,Users as user
ON
user.id = 1 AND
user.id = bb.user_id AND
bb.plannedtrips_id = pt.id AND
pt.coordinates_id = cor.id'
我挣扎了一个小时,我错过了什么? 我的数据库架构如下所示:
我目前正在准备我的查询,我需要在 codeIgnitor 上运行此查询。
答案 0 :(得分:2)
每张桌子都需要单独的LEFT JOIN
。
SELECT
pt.id as planid,
pt.trip_name,
pt.description,
cor.latitude,
cor.longitude,
bb.id as bookmarkid,
bb.num_of_persons as persons
FROM planned_trips as pt
INNER JOIN coordinates as cor ON pt.coordinates_id = cor.id
LEFT JOIN Bookmarkedby as bb ON bb.plannedtrips_id = pt.id
LEFT JOIN Users as user ON user.id = bb.user_id AND user.id = 1
答案 1 :(得分:0)
问题在于LEFT JOIN Bookmarkedby as bb,Users as user
行。考虑将其更改为
FROM
planned_trips as pt
JOIN coordinates as cor ON pt.coordinates_id = cor.id
LEFT JOIN Bookmarkedby as bb ON bb.plannedtrips_id = pt.id
LEFT JOIN Users as user ON user.id = bb.user_id
AND user.id = 1