Mysql
 sql >> Base de données >  >> RDS >> Mysql

Utiliser l'alias d'une requête pour se joindre à une autre table MySQL

Vous pouvez rejoindre kickscooter_control_units sur votre subquery l'utilisation exists mot-clé plutôt qu'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'))  

Il semble que selon votre scénario, vous filtrez l'ID . exists applicable uniquement si vous souhaitez vérifier si certaines subquery contient des valeurs.

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