java - 如何解决where name like '%%' 当name 为null时,查询不到数据?
PHP中文网
PHP中文网 2017-04-18 10:44:56
0
12
1801
PHP中文网
PHP中文网

认证0级讲师

reply all(12)
巴扎黑

SELECT

*

FROM

test

WHERE

1 = 1

AND NAME LIKE concat(

'%',
(
    CASE
    WHEN NAME(传进来的name) IS NULL THEN
        '#################'(数据库name不可能的值)
    ELSE
        NAME(传进来的name)
    END
),
'%')

I wrote a troubleshooting for name, you can check the other one yourself, it may not be the best solution

迷茫

You can write it in business logic, not necessarily in sql

大家讲道理

I don’t know if a database framework is used. If so, there are usually corresponding solutions.
For example, in mybatis, you can use iftest to determine whether the like statement needs to be executed.

Another method is to use the functions of the database itself, such as IF(expr1,expr2,expr3) or IF ELSE

Peter_Zhu

If there are no input parameters at the front end, why do we need to query the results of this field? Doesn't this violate business logic?

Peter_Zhu

where IFNULL(name, '') like'%xx%' and IFNULL(method, '') like '%xx%'
如果namenull将其转换为empty string%%It will match.


But I think the best way is to use SQL的地方增加逻辑判断会比较好一些,改动量应该非常少,只需要几句代码就搞定了。题主可以看看@家常菜 的SQL增加一个where 1=1的条件,那ifconditions and it will be very easy to add conditions.

左手右手慢动作

It’s best to write it in the business logic layer (this layer is what this layer is supposed to do). Don’t handle this kind of thing in the database layer. This is very unreasonable and inefficient.

左手右手慢动作

The simple way is to convert the null value in the table into ''
where ifnull(name,'') like'%%' and ifnull(method,'') like '%%'
If you don’t consider efficiency, this is enough

Ty80

The questioner wants to simply splice the query conditions on the interface into the Where condition of the SQL statement. This is indeed very convenient for program processing. However, for security reasons, it is best not to do this because there is a risk of SQL injection.

If it is an internal application, the environment is indeed very safe. Considering the performance, it is still recommended to process the code logic of splicing SQL. Without this parameter, do not spell the corresponding where clause. For example, if it is Java, it can be written as:

String whereClause = "where xxxxxx"
                     +("".equals(nameStr) ? "" : "name like '%"+nameStr+"%'")
                     +("".equals(methodStr) ? "" : "method like '%"+methodStr+"%'")

*Here are the assumptions:

  1. nameStr and methodStr are the user input content obtained from the interface respectively. If the user does not input, the result will be an empty string instead of null

  2. xxxxxx are other where conditions. If there are really no other conditions, then the "where" string itself needs to be processed*

刘奇

If the database default value is null, or when the obtained value is null, change it to nu. .

Peter_Zhu

where name='' or method ='' or name like'%%' and method like '%%'

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template