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

Uso de alias de una consulta para unirse a otra tabla MySQL

Puedes unirte a kickscooter_control_units en su subquery el uso exists palabra clave en lugar de IN.

select *
from rents r
where 
  exists 
  (select 1
     from support_works sw
     join kickscooters k on k.serial_number = sw.serial_number
     join kickscooter_control_units kcu on  kcu.kickscooter_id =  k.id and kcu.particle_product_id in (9358, 9383)   
     where
       sw.work_type = 'deploy' and
       (sw.updated_at between '2019-11-01 02:00:00' and '2019-11-01 10:00:00'))  

Según su escenario, parece que está filtrando el ID . exists solo aplicable si desea verificar si cierta subquery contiene valores.

 select *
    from rents r
    where 
      r.kickscooter_id in 
      (select k.id
         from support_works sw
         join kickscooters k on k.serial_number = sw.serial_number
         join kickscooter_control_units kcu on  kcu.kickscooter_id =  k.id and kcu.particle_product_id in (9358, 9383)   
         where
           sw.work_type = 'deploy' and
           (sw.updated_at between '2019-11-01 02:00:00' and '2019-11-01 10:00:00'))