gpt4 book ai didi

php - MySQL 获取同一个表中存在对应行的行

转载 作者:行者123 更新时间:2023-11-29 08:21:38 25 4
gpt4 key购买 nike

我试图根据一些基本的 where 子句从表中获取行,现在我想在末尾包含一个“AND EXISTS”子句。我的代码如下:

$stmt = $mysqli->prepare("SELECT object_id FROM ".
$db_table_prefix."user_events
WHERE LPAD(start_timestamp,15,'0') < LPAD(?,15,'0')
AND event_id = ?
AND EXISTS (
SELECT * FROM ".$db_table_prefix."user_events
WHERE LPAD(start_timestamp,15,'0') > LPAD(?,15,'0')
AND event_id = ? )
");

我遇到的问题是我不知道如何在 AND EXISTS 子查询中指定主查询的列值。

我正在寻找一种方法来将这一点添加到子查询中:

AND object_id = **object_id from main query**

感谢任何帮助

最佳答案

此外,还向子查询的表添加了别名以避免混淆

$stmt = $mysqli->prepare("SELECT object_id FROM ".
$db_table_prefix."user_events
WHERE LPAD(start_timestamp,15,'0') < LPAD(?,15,'0')
AND event_id = ?
AND EXISTS (
SELECT * FROM ".$db_table_prefix."user_events AS u
WHERE LPAD(u.start_timestamp,15,'0') > LPAD(?,15,'0')
AND u.object_id = " .$db_table_prefix.".object_id
AND u.event_id = ? )
");

关于php - MySQL 获取同一个表中存在对应行的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19244228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com