我有以下查询可行。它搜索帐户表并按相关性排名返回结果。
SELECT id, name, phone_office, sum( relevance )
FROM (
SELECT id, name, phone_office, 10 AS relevance
FROM accounts
WHERE (
IF( LENGTH( '{$name}' ) >0, name LIKE '%{$name}%', 0 )
)
AND deleted = '0'
UNION
SELECT id, name, phone_office, 5 AS relevance
FROM accounts
WHERE phone_office LIKE '%{$phone}%'
AND deleted = '0'
UNION
SELECT id, name, phone_office, 2 AS relevance
FROM accounts
WHERE billing_address_postalcode LIKE '%{$postal_code}%'
AND deleted = '0'
) results
GROUP BY id
ORDER BY sum( relevance ) DESC
我还需要通过电子邮件地址进行搜索。
问题是我需要添加另一个加入电子邮件地址表的UNION SELECT
。在此应用程序中,帐户电子邮件地址不存储在其自己的email_addresses表中的accounts表中,该表通过名为email_addr_bean_rel的另一个表链接到accounts表。
email_addr_bean_rel具有以下相关字段
id,email_address_id,bean_id,bean_module
在我的例子中,bean_id是帐户的id,而email_address_id是email_addresses表中电子邮件地址行的id。
这是我到目前为止的尝试:
SELECT id as main_id, name, phone_office, sum(relevance)
FROM (
SELECT id, name, phone_office, 10 AS relevance
FROM accounts
WHERE (
IF(LENGTH( '{$name}' ) > 0, name LIKE '%{$name}%', 0)
)
AND deleted = '0'
UNION
SELECT id, name, phone_office, 5 AS relevance
FROM accounts
WHERE phone_office LIKE '%{$phone}%'
AND deleted = '0'
UNION
SELECT id as ac, name, phone_office, 3 AS relevance
FROM accounts
JOIN email_addr_bean_rel eb ON (ac = eb.bean_id)
JOIN email_addresses ea ON ( ea.id = eb.email_address_id )
WHERE ea.email_address LIKE '%{$email}%'
AND accounts.deleted = '0' AND eb.deleted =0
UNION
SELECT id, name, phone_office, 2 AS relevance
FROM accounts
WHERE billing_address_postalcode LIKE '%{$postal_code}%'
AND deleted = '0') results
GROUP BY main_id
ORDER BY sum( relevance ) DESC
我收到列ID模糊错误,但我也不确定如何正确构建电子邮件表的连接。
在下面的一些帮助之后,我现在有了这个:
SELECT *
FROM (
SELECT accounts.id, name, phone_office, email_address, Sum(
CASE
WHEN Length( 'sdfsdfsd' ) >0
AND name LIKE '%sdfsdfsd%'
THEN 10
WHEN phone_office LIKE '%222-055-9034%'
THEN 5
WHEN email_address LIKE '%test@test.com%'
THEN 8
WHEN billing_address_postalcode LIKE '%12345 6789%'
THEN 2
END ) AS relevance
FROM accounts
JOIN email_addr_bean_rel eb ON ( accounts.id = eb.bean_id )
JOIN email_addresses ea ON ( ea.id = eb.email_address_id )
WHERE accounts.deleted = '0'
GROUP BY accounts.id
ORDER BY relevance DESC
) AS temp
WHERE temp.relevance IS NOT NULL
LIMIT 0 , 30
可能不是很有效但是它是唯一可以让它工作而不返回一堆空行以便在mysql中相关的方法
答案 0 :(得分:0)
您的查询将更简单地写为:
SELECT id, name, phone_office, sum( relevance )
FROM (SELECT id, name, phone_office,
(CASE WHEN LENGTH( '{$name}' ) >0 AND name LIKE '%{$name}%' THEN 10
WHEN phone_office LIKE '%{$phone}%' THEN 5
WHEN billing_address_postalcode LIKE '%{$postal_code}%' THEN 2
END) AS relevance
FROM accounts
WHERE deleted = '0'
) results
WHERE relevance IS NOT NULL
GROUP BY id
ORDER BY sum( relevance ) DESC ;
然后可以简化为不使用子查询:
SELECT id, name, phone_office,
sum(CASE WHEN LENGTH( '{$name}' ) >0 AND name LIKE '%{$name}%' THEN 10
WHEN phone_office LIKE '%{$phone}%' THEN 5
WHEN billing_address_postalcode LIKE '%{$postal_code}%' THEN 2
END) AS relevance
FROM accounts
WHERE deleted = '0'
GROUP BY id
ORDER BY relevance DESC ;
使用此版本,额外的JOIN
应该很容易。