MySQL: esta versión de MySQL aún no admite la subconsulta 'LIMIT & IN/ALL/ANY/SOME

Resuelto Mihai Viteazu asked hace 11 años • 6 respuestas

Este es el phpcódigo que estoy usando.

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

este es el error que me da

 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
Mihai Viteazu avatar Jul 27 '13 06:07 Mihai Viteazu
Aceptado

En lugar de usar IN, puedes usar JOIN

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
Mike Brant avatar Jul 27 '2013 00:07 Mike Brant

Puede utilizar lo siguiente para evitar este error.

$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
');
Surender avatar Jul 16 '2014 09:07 Surender

No necesita una subconsulta aquí. Prueba esto:

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

En MySQL 5.0.26 y posteriores, obtendrá un error:

MySQL no admite LIMIT en subconsultas para ciertos operadores de subconsulta:

Referencia .

echo_Me avatar Jul 26 '2013 23:07 echo_Me

agrega esta es tu condición

(SELECT * FROM (
    SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC)
Vasim Walikar avatar Feb 17 '2020 13:02 Vasim Walikar