我有一個複雜的查詢需要從總共4個表中的字段。我有一個內部聯接語句,它有一個OR
子句,這大大減緩了查詢的速度。如何擺脫內部連接OR條件
這是我的查詢:
SELECT
pending_corrections.sightinguid AS 'pending_corrections_sightinguid',
vehicle_ownership.id AS 'fk_vehicle_owner',
@bill_id AS 'fk_bills',
@nullValue AS 'fk_final_sightings_sightinguid',
TRIM(pending_corrections.corrected_plate) AS 'vrn',
pending_corrections.seenDate AS 'seen_date',
cameras.in_out AS 'in_out',
vehicle_vrn.fk_sysno AS 'fk_sysno',
cameras.zone AS 'fk_zones',
'0' AS 'auto_generated'
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
OR pending_corrections.corrected_plate = vehicle_vrn.vrnno)
INNER JOIN
vehicle_ownership ON vehicle_vrn.fk_sysno = vehicle_ownership.fk_sysno
WHERE
pending_corrections.corrected_plate <> ''
AND pending_corrections.corrected_plate IS NOT NULL
AND pending_corrections.unable_to_correct <> '1'
AND pending_corrections.seenDate >= @dateFrom
AND pending_corrections.seenDate <= @dateTo
AND (cameras.in_out = 1 OR cameras.in_out = 0)
AND cameras.zone IN (SELECT
zone_number
FROM
zones
WHERE
fk_site = @siteId)
AND seenDate >= vehicle_vrn.vrn_start_date
AND (seenDate <= vehicle_vrn.vrn_end_date
OR vehicle_vrn.vrn_end_date IS NULL
OR vehicle_vrn.vrn_end_date = '0001-01-01 00:00:00')
AND seenDate >= vehicle_ownership.ownership_start_date
AND (seenDate <= vehicle_ownership.ownership_end_date
OR vehicle_ownership.ownership_end_date IS NULL
OR vehicle_ownership.ownership_end_date = '0001-01-01 00:00:00')
ORDER BY pending_corrections.corrected_plate , pending_corrections.seenDate ASC;
我怎樣才能實現的加入一個相同的效果,但沒有OR
? OR
子句的原因是因爲pending_corrections.corrected_plate
值必須與vehicle_vrn
表中的vrn500
或vrnno
列匹配。
這是您的最後一個問題的副本,然後您在發佈答案後更改它。來吧。 – Paparazzi
不是@Frisbee - 這個問題更具體,因爲它只處理內部連接的OR部分,而不是整個查詢本身。 –
從複製品「我怎樣才能達到相同的效果,但在一個連接中沒有OR?」 – Paparazzi