我想选择images表中存在但不在images_viewed表中的行。它们都有相同的名称。
这是我正在使用的代码。它有效,但它使我的网站很慢。
$next = $mysqli->query("SELECT `name` FROM `images` WHERE `name` NOT IN (SELECT `name` FROM `images_viewed` WHERE `ip` = '$ip') ORDER BY RAND() LIMIT 1");
if($next->num_rows == 1){
$fetch_next = $next->fetch_assoc();
$next_name = $fetch_next['name'];
}
我该如何修复此代码?我可以使用更好的选择语句吗?
答案 0 :(得分:2)
尝试:
SELECT images.name FROM images
LEFT OUTER JOIN images_viewed
ON images.name = images_viewed.name
WHERE images_viewed.ANY_OTHER_FIELD IS NULL AND images_views.ip = {$ip}