我有一个使用SUBSTRING()
作为标准的查询:
SELECT p.name p_name,
pa.line1 p_line1,
pa.zip p_zip,
c.name c_name,
ca.line1 c_line1,
ca.zip c_zip
FROM bank b
JOIN import_bundle ib ON ib.bank_id = b.id
JOIN generic_import gi ON gi.import_bundle_id = ib.id
JOIN account_import ai ON ai.generic_import_id = gi.id
JOIN account a ON a.account_import_id = ai.id
JOIN account_address aa ON aa.account_id = a.id
JOIN address ca ON aa.address_id = ca.id
JOIN address pa ON pa.zip = ca.zip OR (pa.zip = ca.zip AND pa.line1 = ca.line1)
JOIN prospect p ON p.address_id = pa.id
JOIN customer c ON a.customer_id = c.id
WHERE b.name = 'M'
AND ib.active = 1
AND gi.active = 1
AND SUBSTRING(p.name, 1, 12) = SUBSTRING(c.name, 1, 12)
LIMIT 100
如您所见,它只是比较p.name
和c.name
的前12个字符。不幸的是,将此查询添加到WHERE
子句会使我的查询无法忍受。有没有任何技巧可以做同样的比较,或者我最好的办法是在每个包含客户姓名前12个字符的表中添加另一列?我希望不是后者,因为这将是很多工作,我最终会做这样的几个比较。
答案 0 :(得分:4)
添加额外的列并设置更新触发器以自动填充它们。当然,请务必在新列上创建索引。