我有以下查詢:MySQL的多個或在WHERE
SELECT
b.business_name,
b.address,
b.city,
b.state,
b.phone_number,
i.date
FROM business_table b
LEFT JOIN inspection_table i ON b.id = i.business_id
WHERE i.date = (
SELECT MAX(i.date)
FROM inspection_table i
WHERE b.id = i.business_id
)
AND b.city LIKE '%Boston%'
OR b.city LIKE '%Dallas%'
OR b.city LIKE '%Seattle%'
OR b.city LIKE '%New York%'
OR b.city LIKE '%Portland%'
OR b.city LIKE '%San Antonio%'
OR b.city LIKE '%Los Angeles%'
OR b.city LIKE '%Miami%'
ORDER BY i.date DESC;
我得到的結果我之後,但在查詢速度很慢(〜17S)。有沒有更好的方法來構造這個查詢?城市字段在business_table中編入索引。
你** **可以使用'REGEXP'('b.city REGEXP「波士頓|達拉斯| Seattle''),但會實際上可能會稍微慢一些。它不應該花17秒,你的桌子有多少行? –
〜我的表中有32,000行 – MoreScratch
您是否嘗試過將括號放在您的OR列表中;還是你想要「最近來自波士頓...以及其他所有城市的一切」? – Uueerdo