Php Mysql:使用多个“where”连接3个表?
我试图在一个条件下将一个表连接到另一个表,然后在另一个条件下将该表连接到另一个表。您可以这样做吗?如果可以,正确的语法是什么?我一直在尝试这个,但它不起作用:Php Mysql:使用多个“where”连接3个表?,php,mysql,database,Php,Mysql,Database,我试图在一个条件下将一个表连接到另一个表,然后在另一个条件下将该表连接到另一个表。您可以这样做吗?如果可以,正确的语法是什么?我一直在尝试这个,但它不起作用: $check = $members->prepare("select users.fname, users.lname, groups.groupid, attributes.max from users JOIN groups on users.user_id = groups.userid
$check = $members->prepare("select users.fname, users.lname, groups.groupid, attributes.max
from users
JOIN groups
on users.user_id = groups.userid
where groups.userid = ?
LEFT JOIN attributes
on users.user_id = attributes.userid
where attributes.groupid = ?");
$check->bind_param('ss', $_SESSION['token'][1], $which_group);
$check->execute();
干杯。对attributes.groupid的测试必须是左侧联接的联接条件的一部分。尝试在WHERE子句中测试此条件将强制左连接的行为就像它是内部连接一样
select users.fname, users.lname, groups.groupid, attributes.max
from users
JOIN groups
on users.user_id = groups.userid
LEFT JOIN attributes
on users.user_id = attributes.userid
and attributes.groupid = ?
where groups.userid = ?
attributes.groupid的测试必须是左侧联接的联接条件的一部分。尝试在WHERE子句中测试此条件将强制左连接的行为就像它是内部连接一样
select users.fname, users.lname, groups.groupid, attributes.max
from users
JOIN groups
on users.user_id = groups.userid
LEFT JOIN attributes
on users.user_id = attributes.userid
and attributes.groupid = ?
where groups.userid = ?
就用。。。。。。。。及
就用。。。。。。。。及
如果您引用的条件是一个文本,并且与联接表没有任何关系,请在一个where子句中组合它们
select users.fname, users.lname, groups.groupid, attributes.max
from users
JOIN groups
on users.user_id = groups.userid
LEFT JOIN attributes
on users.user_id = attributes.userid
where groups.userid = ? and attributes.groupid = ?
如果您引用的条件是一个文本,并且与联接表没有任何关系,请在一个where子句中组合它们
select users.fname, users.lname, groups.groupid, attributes.max
from users
JOIN groups
on users.user_id = groups.userid
LEFT JOIN attributes
on users.user_id = attributes.userid
where groups.userid = ? and attributes.groupid = ?
您只能有一个WHERE子句,但可以在其中放置多个条件。请参阅如何编写该语句的数据库文档,特别是在JOIN上下文中。您只能有一个WHERE子句,但可以在其中放入多个条件。请参阅您使用的数据库文档,了解如何编写,特别是在JOIN上下文中。