[英]Multiple left joins in a query
So, i have this query right here: 所以,我在这里有这个查询:
$query1 = 'SELECT users.first_name,users.last_name,users.phone, matching.date, matching.points, matching.time, matching.epos_id,rbpos_epos.epos_id,rbpos_epos.location
FROM users LEFT JOIN matching WHERE users.user_id="'.$id_user.'"
AND users.user_id=matching.user_id
LEFT JOIN rbpos_epos WHERE rbpos_epos.epos_id=matching.epos_id';
Which is giving me an error, what i'm trying to do in this last line: 这给了我一个错误,这是我在最后一行中试图做的事情:
LEFT JOIN rbpos_epos WHERE rbpos_epos.epos_id=matching.epos_id';
is to get the location for that specific epos_id. 是获取该特定epos_id的位置。
It means , in the matching table, i have a epos_id column, and in the rbpos_epos column i have an epos_id field which beside has a location field.. i need to get the respective location for each epos_id.. 这意味着,在匹配表中,我有一个epos_id列,在rbpos_epos列中,我有一个epos_id字段,该字段旁边还有一个位置字段。我需要获取每个epos_id的相应位置。
What am i doing wrong? 我究竟做错了什么? Thanks.. 谢谢..
This one works 这个作品
$query1 = 'SELECT users.first_name,users.last_name,users.phone, matching.date, matching.points, matching.time,matching.location
FROM users ,matching WHERE users.user_id="'.$id_user.'"
AND users.user_id=matching.user_id';
I need to get the location from rbpos_epos table. 我需要从rbpos_epos表中获取位置。 The two tables rbpos_epos and matching have both a epos_id field 两个表rbpos_epos和match都具有epos_id字段
So what i need is to find the location in rbpos_epos based on the epos_id i have at matching.. 因此,我需要根据匹配时使用的epos_id在rbpos_epos中找到位置。
Try this :: 尝试这个 ::
SELECT users.first_name,users.last_name,users.phone, matching.date, matching.points, matching.time, matching.epos_id,rbpos_epos.epos_id,rbpos_epos.location
FROM users
INNER JOIN matching ON users.user_id=matching.user_id
INNER JOIN rbpos_epos ON rbpos_epos.epos_id=matching.epos_id
WHERE users.user_id="'.$id_user.'"
Use ON
instead of WHERE
in your LEFT JOIN
: 在您的LEFT JOIN
使用ON
代替WHERE
:
$query1 = 'SELECT users.first_name,users.last_name,users.phone, matching.date, matching.points, matching.time, matching.epos_id,rbpos_epos.epos_id,rbpos_epos.location
FROM users LEFT JOIN matching ON users.user_id="'.$id_user.'"
AND users.user_id=matching.user_id
LEFT JOIN rbpos_epos ON rbpos_epos.epos_id=matching.epos_id';
And put any WHERE
clauses after the LEFT JOIN
s 并将所有WHERE
子句放在LEFT JOIN
之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.