PHP PDO在同一查询中查询两个表
我有这个疑问PHP PDO在同一查询中查询两个表,php,mysql,Php,Mysql,我有这个疑问 $sth = $db->prepare( 'SELECT shout_id FROM shout_info' . ' WHERE shout_location = "3"' . ' AND (shout_sex = "0" OR shout_sex = ?)' . ' AND (shout_age = "0" OR shout_age = ?)' . ' AND shout_location_spec = ? AND user_id &
$sth = $db->prepare(
'SELECT shout_id FROM shout_info'
. ' WHERE shout_location = "3"'
. ' AND (shout_sex = "0" OR shout_sex = ?)'
. ' AND (shout_age = "0" OR shout_age = ?)'
. ' AND shout_location_spec = ? AND user_id <> ?'
. ' ORDER BY date_created DESC LIMIT 15'
);
$sth->bindValue(1, $user_sex);
$sth->bindValue(2, $sql_general_age);
$sth->bindValue(3, $user_workplace);
$sth->bindValue(4, $user_id);
$sth->execute();
$workplace_array = $sth->fetchAll(PDO::FETCH_ASSOC);
$sth=$db->prepare(
'从shout\u信息中选择shout\u id'
“WHERE_location=“3””
和(shout_sex=“0”或shout_sex=?)
和(shout_age=“0”或shout_age=?)
“并喊出位置和用户id?”
.“按日期创建的订单说明限制15”
);
$sth->bindValue(1,$user\u sex);
$sth->bindValue(2$sql\u general\u age);
$sth->bindValue(3$user\u workplace);
$sth->bindValue(4,$user\u id);
$sth->execute();
$workplace\u array=$sth->fetchAll(PDO::FETCH\u ASSOC);
它相当长,但我想知道如何从另一个表中的列进行检查检查检查将是和shout_approved=“1”
但是shout_approved在表shout_状态中,shout_状态的主要id是shout_id,就像shout_info中一样,因此,我不确定如何使用与原始查询中检查的id相同的id检查此表。
对不起,我正在尽可能清楚地解释
如果您还需要什么,请告诉我您需要一个
左侧外部连接
,用于此:
SELECT
s.shout_id
FROM
shout_info s
LEFT OUTER JOIN
shout_status ss
ON
ss.shout_id = s.shout_id
WHERE
s.shout_location = "3" AND
(s.shout_sex = "0" OR s.shout_sex = ?) AND
(s.shout_age = "0" OR s.shout_age = ?) AND
s.shout_location_spec = ? AND
s.user_id <> ? AND
ss.shout_approved = "1"
ORDER BY
s.date_created DESC
LIMIT
15
选择
s、 呼喊
从…起
呼喊信息
左外连接
呼喊状态
在…上
ss.shout\U id=s.shout\U id
哪里
s、 呼喊_location=“3”和
(s.shout_sex=“0”或s.shout_sex=?)和
(s.shout_age=“0”或s.shout_age=?)和
s、 呼叫位置规格=?及
s、 用户id?及
ss.shout_已批准=“1”
订购人
s、 创建日期描述
极限
15
这应该可以:
SELECT
shout_id
FROM
shout_info
WHERE shout_location = "3"
AND (shout_sex = "0" OR shout_sex = ?)
AND (shout_age = "0" OR shout_age = ?)
AND shout_location_spec = ? AND user_id <> ?
ORDER BY date_created DESC LIMIT 15'
Join shout_approved on shout_approved.shout_id = shout_id
where shout_approved = "1"
选择
呼喊
从…起
呼救信息
何处显示位置=“3”
和(shout_sex=“0”或shout_sex=?)
和(shout_age=“0”或shout_age=?)
和shout_location_spec=?用户id呢?
订单日期\u创建描述限制15'
在shout\u approved上加入shout\u approved。shout\u id=shout\u id
其中,shout_approved=“1”
尽管您的查询可能会工作,但您应该始终使用显式连接,否则DBMS可能需要做更多的工作来确定您所暗示的连接类型,并且可以轻松地以指数方式增加查询时间。