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

时间:2013-07-26 23:52:39

标签: php mysql

这是使用

的代码
    $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

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

5 个答案:

答案 0 :(得分:122)

您可以使用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

答案 1 :(得分:111)

您可以使用以下方法绕过此错误。

$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
');

答案 2 :(得分:6)

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

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

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

  

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

Reference

答案 3 :(得分:0)

为什么你不能使用简单的:?

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

这里的子查询是什么?

答案 4 :(得分:0)

添加这是您的状况

(SELECT * FROM (
    SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC)