gpt4 book ai didi

MySQL : sql select query if condition parameter is null then ignore the parameter

转载 作者:行者123 更新时间:2023-11-29 05:58:44 25 4
gpt4 key购买 nike

我有一个Mysql查询

SELECT *
FROM EMPLOYEE
WHERE DEPARTMENT = ?
AND DESIGNATION = ?
AND DISTRICT = ?
AND CIRCLE = ?

并且很可能参数 (?) 中的任何 1 或 2 或 3 可以为空或 null。

so what should I do so that the empty parameters are totally Ignore in the where clause and only search for the non-empty parameter in the table.

我怎样才能做到这一点

请帮忙...查询必须兼容mysql。谢谢

最佳答案

您可以添加两次输入参数并进行如下查询:

SELECT *
FROM EMPLOYEE
WHERE (DEPARTMENT = ? OR ? IS NULL)
AND (DESIGNATION = ? OR ? IS NULL)
AND (DISTRICT = ? OR ? IS NULL)
AND (CIRCLE = ? OR ? IS NULL)

那么在第一个和第二个 '?'你绑定(bind)相同的值。

或者(可以说更好),您可以动态构建 SQL,如果不适用,则省略 where 子句的某些部分。您可以连接字符串以获取基本 SQL 并仍然使用绑定(bind)参数。

关于MySQL : sql select query if condition parameter is null then ignore the parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46941690/

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