sql >> Base de Datos >  >> RDS >> Mysql

Mysql accede a la tabla principal dentro de la subconsulta unida a la derecha

Usar:

    SELECT a.id,
           a.region 
      FROM ads a    
RIGHT JOIN (SELECT t.*,
                   CASE 
                      WHEN @region = t.region THEN @rownum := @rownum + 1
                      ELSE @rownum := 1
                   END as rank,
                   @region := t.region
              FROM ADS t
              JOIN (SELECT @rownum := 0, @region := 1) r
          ORDER BY t.region) b ON b.id = a.id
                              AND b.region = a.region
                              AND b.rank <= 4