119

这是我使用的代码

    $Last_Video         = $db->fetch_all('
    SELECT VID, thumb
    FROM video
    WHERE VID IN (
        SELECT VID
        FROM video
        WHERE title LIKE "%'.$Channel['name'].'%"
        ORDER BY viewtime DESC
        LIMIT 5)
    ORDER BY RAND()
    LIMIT 1
');

这是给我的错误

 Message:   Error during SQL execution: SELECT VID, thumb FROM video WHERE VID IN ( SELECT VID FROM video WHERE title LIKE "%funny%" ORDER BY viewtime DESC LIMIT 5) ORDER BY RAND() LIMIT 1<br />
 MySQL Error:   This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'<br />
MySQL Errno:    1235

我该如何解决这个问题?它的另一种方法......所以我没有得到错误......

4

6 回答 6

184

您可以使用 JOIN 而不是使用 IN

SELECT v.VID, v.thumb
FROM video AS v
INNER JOIN
     (SELECT VID
     FROM video
     WHERE title LIKE "%'.$Channel['name'].'%"
     ORDER BY viewtime DESC
     LIMIT 5) as v2
  ON v.VID = v2.VID
ORDER BY RAND()
LIMIT 1
于 2013-07-27T00:10:26.847 回答
157

您可以使用以下内容绕过此错误。

$Last_Video = $db->fetch_all('
    SELECT VID, thumb
    FROM video
    WHERE VID IN (select * from (
        SELECT VID
        FROM video
        WHERE title LIKE "%'.$Channel['name'].'%"
        ORDER BY viewtime DESC
        LIMIT 5) temp_tab)
    ORDER BY RAND()
    LIMIT 1
');
于 2014-07-16T09:47:41.823 回答
5

您在这里不需要子查询。尝试这个:

 SELECT VID, thumb
 FROM video
 WHERE title LIKE "%'.$Channel['name'].'%"
 ORDER BY RAND() DESC
 LIMIT 1

在 MySQL 5.0.26 及更高版本中,您将收到错误:

MySQL 不支持某些子查询运算符的子查询中的 LIMIT:

参考

于 2013-07-26T23:58:20.453 回答
0

为什么你不能使用 simple: ?

SELECT v.VID, v.thumb
FROM video as v
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5

这里的子查询是什么?

于 2013-10-25T08:46:45.423 回答
0

添加这是您的状况

(SELECT * FROM (
    SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC)
于 2020-02-17T13:11:29.907 回答
0

mysql被禁用阅读它ORACLE

DELETE FROM wall_orders WHERE order_id IN (
SELECT order_id FROM (SELECT order_id, COUNT(orders_products_id) as cnt FROM wall_orders_productsGROUP BY order_id ORDER BY cnt DESC LIMIT 1000) y1 WHERE cnt > 170 LIMIT 1000)

235 - 这个版本的 MariaDB 还不支持 'LIMIT & IN/ALL/ANY/SOME 子查询'

简单的 SQL 不可能

于 2021-12-26T17:46:56.067 回答