gpt4 book ai didi

MySQL 搜索多列,但强制执行一个条件

转载 作者:行者123 更新时间:2023-11-29 07:28:03 24 4
gpt4 key购买 nike

我正在尝试编写一个查询来搜索数据库中的多个列,但是,我需要强制在这些条件之上匹配 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%'

有什么办法可以实现这个目标吗?非常感谢!

最佳答案

这是否会给您带来预期的结果:

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%')

关于MySQL 搜索多列,但强制执行一个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com