Php 使用排除数据库中的行!=在PDO查询中
我试图用PDO查询排除某些行,但它没有返回正确的值,而且我没有看到我的错误,也许你们中的一些人可以帮助我 这是第一个有效的查询Php 使用排除数据库中的行!=在PDO查询中,php,mysql,pdo,Php,Mysql,Pdo,我试图用PDO查询排除某些行,但它没有返回正确的值,而且我没有看到我的错误,也许你们中的一些人可以帮助我 这是第一个有效的查询 $objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid'); 现在我想排除从这个查询中得到的chatid foreach ($getRecievedChatFtch as $
$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');
现在我想排除从这个查询中得到的chatid
foreach ($getRecievedChatFtch as $chatid) {
echo $chatid['chatid'] . '<BR>';
}
Wich是正确的,我想排除这两个值,因此执行下一个查询:
$objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = :ownerid AND chatid != :chatid GROUP BY chatid');
foreach ($getSendChat as $key ) {
echo $key['chatid'] . '<BR>';
}
这个值44495
是正确的,尽管我只需要它一次(这就是为什么我GROUP BY chatid
),但是20920
是我需要排除的值之一
有人知道我做错了什么吗
提前谢谢
全部代码:
//Voor de berichten die je hebt ontvangen.
$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');
$objGetRecievedChat->bindParam('recieverid', $member_id);
$objGetRecievedChat->execute();
$getRecievedChatFtch = $objGetRecievedChat->fetchAll(PDO::FETCH_ASSOC);
//Dit is voor verzonden berichten.
foreach ($getRecievedChatFtch as $chatid) {
echo $chatid['chatid'] . '<BR>';
$objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = :ownerid AND chatid NOT IN(:chatid) GROUP BY chatid');
$objGetSendChat->bindParam('ownerid', $member_id);
$objGetSendChat->bindParam('chatid', $chatid['chatid']);
$objGetSendChat->execute();
$getSendChat = $objGetSendChat->fetchAll(PDO::FETCH_ASSOC);
foreach ($getSendChat as $key) {
echo $key['chatid'] . '<BR>';
}
}
//Voor de berichten die je hebt ontvangen。
$objGetRecievedChat=$objDatabaseMessages->prepare('SELECT*FROM messages,其中receiverid=:receiverid GROUP BY chatid');
$objGetRecievedChat->bindParam('receiverid',$member\u id);
$objGetRecievedChat->execute();
$GetReceivedChatftch=$ObjGetReceivedChat->fetchAll(PDO::FETCH_ASSOC);
//这是沃尔·弗桑登·贝里赫滕。
foreach($GetReceivedChatftch作为$chatid){
echo$chatid['chatid']。
;
$objGetSendChat=$objDatabaseMessages->prepare('SELECT*FROM messages,其中ownerid=:ownerid和chatid不在(:chatid)组中,按chatid');
$objGetSendChat->bindParam('ownerid',$member\u id);
$objGetSendChat->bindParam('chatid',$chatid['chatid']);
$objGetSendChat->execute();
$getSendChat=$objGetSendChat->fetchAll(PDO::FETCH_ASSOC);
foreach($getSendChat作为$key){
echo$key['chatid']。
;
}
}
您弄错了:在foreach循环中,您检索除当前行以外的所有行。您必须将查询移出foreach
,并使用WHERE IN
//Voor de berichten die je hebt ontvangen.
$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');
$objGetRecievedChat->bindParam('recieverid', $member_id);
$objGetRecievedChat->execute();
$getRecievedChatFtch = $objGetRecievedChat->fetchAll(PDO::FETCH_ASSOC);
//Dit is voor verzonden berichten.
$chatids = array();
foreach ($getRecievedChatFtch as $chatid) {
echo $chatid['chatid'] . '<BR>';
$chatids = $chatid['chatid'];
}
$placeholders = implode(',', array_fill('?', count($chatids)));
$objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = ? AND chatid NOT IN(' . $placeholders . ') GROUP BY chatid');
$objGetSendChat->execute(array_merge(array($ownerid, $chatids)));
$getSendChat = $objGetSendChat->fetchAll(PDO::FETCH_ASSOC);
foreach ($getSendChat as $key) {
echo $key['chatid'] . '<BR>';
}
//Voor de berichten die je hebt ontvangen。
$objGetRecievedChat=$objDatabaseMessages->prepare('SELECT*FROM messages,其中receiverid=:receiverid GROUP BY chatid');
$objGetRecievedChat->bindParam('receiverid',$member\u id);
$objGetRecievedChat->execute();
$GetReceivedChatftch=$ObjGetReceivedChat->fetchAll(PDO::FETCH_ASSOC);
//这是沃尔·弗桑登·贝里赫滕。
$chatids=array();
foreach($GetReceivedChatftch作为$chatid){
echo$chatid['chatid']。
;
$chatids=$chatid['chatid'];
}
$placeholders=内爆(“,”,数组填充(“?”,计数($chatids));
$objGetSendChat=$objDatabaseMessages->prepare('SELECT*FROM messages,其中ownerid=?和chatid不在('.$placeholders.')组中,按chatid');
$objGetSendChat->execute(array_merge(array($ownerid,$chatids));
$getSendChat=$objGetSendChat->fetchAll(PDO::FETCH_ASSOC);
foreach($getSendChat作为$key){
echo$key['chatid']。
;
}
或多或少地(因为我不喜欢在prepared语句中使用WHERE IN
。通常可以使用JOIN
来避免它们
$objGetSendChat=…
当您在foreach
中使用$getSendChat
时
所以我觉得我们缺少一些代码,其中包含错误
另外,您通过chatid对
进行分组,结果中会出现两次44495
,因此结果不能是查询的结果。将查询更改为catid NOT in(xxxx,xxxx)。您不使用PD排除,PDO在这里无关紧要。您只需使用查询本身排除。如果您有一个数据数组,您可以在chatid不在的位置使用(:chatid)
如果您按chatid进行分组,则无法将同一个chatid重复两次。我觉得您没有分享整个故事。我不明白,有一个数组包含2个元素,您要排除,而您的查询只有1个chatid!=:chatid
。如何绑定变量?您可能需要chatid不在其中(…,…)
。我正在使用bindParam绑定变量。我只是在做一个fetchall。$getSendChat=$objGetSendChat->fetchall(PDO::FETCH\u ASSOC);如果GROUP BY
列中有两个相同的值,则表示结果不是查询中的结果,因此您应该编辑问题并显示整个代码。更新了我的原始注释。现在这样更好了。每次在foreach
中启动查询时,您都会得到除当前行以外的所有行,从而得到您的结果。仍然给出t他得到了和以前一样的结果。
//Voor de berichten die je hebt ontvangen.
$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');
$objGetRecievedChat->bindParam('recieverid', $member_id);
$objGetRecievedChat->execute();
$getRecievedChatFtch = $objGetRecievedChat->fetchAll(PDO::FETCH_ASSOC);
//Dit is voor verzonden berichten.
foreach ($getRecievedChatFtch as $chatid) {
echo $chatid['chatid'] . '<BR>';
$objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = :ownerid AND chatid NOT IN(:chatid) GROUP BY chatid');
$objGetSendChat->bindParam('ownerid', $member_id);
$objGetSendChat->bindParam('chatid', $chatid['chatid']);
$objGetSendChat->execute();
$getSendChat = $objGetSendChat->fetchAll(PDO::FETCH_ASSOC);
foreach ($getSendChat as $key) {
echo $key['chatid'] . '<BR>';
}
}
//Voor de berichten die je hebt ontvangen.
$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');
$objGetRecievedChat->bindParam('recieverid', $member_id);
$objGetRecievedChat->execute();
$getRecievedChatFtch = $objGetRecievedChat->fetchAll(PDO::FETCH_ASSOC);
//Dit is voor verzonden berichten.
$chatids = array();
foreach ($getRecievedChatFtch as $chatid) {
echo $chatid['chatid'] . '<BR>';
$chatids = $chatid['chatid'];
}
$placeholders = implode(',', array_fill('?', count($chatids)));
$objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = ? AND chatid NOT IN(' . $placeholders . ') GROUP BY chatid');
$objGetSendChat->execute(array_merge(array($ownerid, $chatids)));
$getSendChat = $objGetSendChat->fetchAll(PDO::FETCH_ASSOC);
foreach ($getSendChat as $key) {
echo $key['chatid'] . '<BR>';
}