sql >> Database teknologi >  >> RDS >> Mysql

Sådan håndteres flere joinforbindelser

Omskrivning som en UNION er enkelt, kopier kilden og fjern en af ​​OR ed betingelser i hver:

SELECT 
   pending_corrections.corrected_plate , pending_corrections.seenDate
FROM
    (pending_corrections
    INNER JOIN cameras ON pending_corrections.camerauid = cameras.camera_id)
        INNER JOIN
    vehicle_vrn ON (pending_corrections.corrected_plate = vehicle_vrn.vrn500)
        INNER JOIN
    vehicle_ownership ON vehicle_vrn.fk_sysno = vehicle_ownership.fk_sysno
WHERE
    pending_corrections.seenDate >= '2015-01-01 00:00:00'
        AND pending_corrections.seenDate <= '2015-01-31 23:59:59'

union 

SELECT 
   pending_corrections.corrected_plate , pending_corrections.seenDate
FROM
    (pending_corrections
    INNER JOIN cameras ON pending_corrections.camerauid = cameras.camera_id)
        INNER JOIN
    vehicle_vrn ON pending_corrections.corrected_plate = vehicle_vrn.vrnno)
        INNER JOIN
    vehicle_ownership ON vehicle_vrn.fk_sysno = vehicle_ownership.fk_sysno
WHERE
    pending_corrections.seenDate >= '2015-01-01 00:00:00'
        AND pending_corrections.seenDate <= '2015-01-31 23:59:59'

ORDER BY 1,2;

Er der et indeks på pending_corrections.seenDate ?



  1. Forespørgselstabel med array_agg/median for ALLE tidligere positioner, LAST_10, LAST_50, eksklusive nuværende position

  2. Indstil drift af MySQL

  3. Migration udenlandsk nøgle vs veltalende forhold i Laravel

  4. Fejl ved installation af pg gem, mkmf.rb kan ikke finde header-filer til ruby ​​(Mac OSX 10.6.5)