我的第一个查询获得了所有相关的 ID Table A
,另一个查询获得了所有必要ids
的Table B
.
现在我想通过在两个查询上方不匹配的那些 id 进行第三次查询。
例子:
- 查询返回:1、5、10、15。
- 查询返回:5。
- 查询要按:1、10、15。
这是我的工作:
//1st query:
foreach((array)$f2_ids as $indx => $value) {
$g = mysqli_query($dbh,"SELECT id FROM update WHERE
`to_id`='".$pg_ids[$indx]."' AND `from_id`='".$f2_ids[$indx]."' GROUP BY id") or die(mysqli_error($dbh));
while ($rows = mysqli_fetch_assoc($g)) {
$ids[] = $rows['id'];
}
}
//2st query:
foreach((array)$ids as $id) {
$p = mysqli_query($dbh,"SELECT post_id FROM view WHERE `post_id`='".$id."' AND `user_id` ='".$session->id."'") or die(mysqli_error($dbh));
while ($rows = mysqli_fetch_assoc($p)) {
$Vids[] = $rows['post_id'];
}
}
//3st query: I tried which not get proper result.
foreach((array)$ids as $index => $value) {
if($ids[$index] !== $Vids[$index]){ // avoid match ids
//echo $ids[$index];
$j = mysqli_query($dbh,"SELECT * FROM update WHERE `id`='".$ids[$index]."' ORDER BY created DESC");
}
}
更新:
foreach((array)$ids as $id) {
$p = mysqli_query($dbh,"SELECT *
FROM update
WHERE `id` = '$id'
AND `id` NOT IN (SELECT post_id
FROM view
WHERE `post_id`='$id'
AND `user_id`='$myid')
ORDER BY created DESC") or die(mysqli_error($dbh));
$rows = mysqli_fetch_assoc($p);
$id = $rows['id'];
echo $id;
}