我正在尝试编写一个查询来搜索我的数据库中的多个列,但是,我需要强制在这些条件之上匹配ID:
SELECT users.id, users.first_name, users.last_name, users.email, fax_did.fax_did,users.enterprise_id FROM users
INNER JOIN users_groups ON users.id = users_groups.user_id
INNER JOIN groups ON users_groups.group_id = groups.id
INNER JOIN product ON groups.id = product.groupid
INNER JOIN fax_did ON product.productid = fax_did.productid
WHERE users.enterprise_id = 2
AND users.first_name LIKE '%test%'
OR users.last_name LIKE '%test%'
OR users.email LIKE '%test%'
OR fax_did.fax_did LIKE '%239214%'
我需要搜索条件来返回与enterprise_id匹配的结果,如下所示:
WHERE users.enterprise_id = 2
但是,其他搜索条件可能是可选的:
AND users.first_name LIKE '%test%'
OR users.last_name LIKE '%test%'
OR users.email LIKE '%test%'
OR fax_did.fax_did LIKE '%239214%'
有没有办法实现这个目标?非常感谢你!
答案 0 :(得分:2)
这能否为您提供您期望的结果:
SELECT users.id, users.first_name, users.last_name, users.email, fax_did.fax_did,users.enterprise_id FROM users
INNER JOIN users_groups ON users.id = users_groups.user_id
INNER JOIN groups ON users_groups.group_id = groups.id
INNER JOIN product ON groups.id = product.groupid
INNER JOIN fax_did ON product.productid = fax_did.productid
WHERE users.enterprise_id = 2
AND (users.first_name LIKE '%test%'
OR users.last_name LIKE '%test%'
OR users.email LIKE '%test%'
OR fax_did.fax_did LIKE '%239214%')