2个表查询的LEFT JOIN是失败

时间:2016-09-23 22:58:35

标签: php mysql codeigniter codeigniter-3

我正在尝试获取两个表的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'

我挣扎了一个小时,我错过了什么? 我的数据库架构如下所示:

schema1

我目前正在准备我的查询,我需要在 codeIgnitor 上运行此查询。

2 个答案:

答案 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