0

我想选择存在于 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'];
        }

如何修复此代码?我可以使用更好的选择语句吗?

4

1 回答 1

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}
于 2013-11-09T02:47:37.540 回答