数据📊
id | name | parent_id |
---|---|---|
1 | 党组织 | 0 |
2 | 陕西党委 | 1 |
3 | 1 | |
4 | Null | 2 |
5 | 渭南党委 | Null |
实验⁉️
查询int类型不为空的字段
select * from org where parent_id <> ""
❌mybatis 中的 if判断会把整形中的 0 识别为false(空)
select * from org where parent_id != ""
❌mybatis 中的 if判断会把整形中的 0 识别为false(空)
select * from org where !ISNULL(parent_id)
✔️
查询int类型为空的字段
select * from org where parent_id = ""
mybatis 中的 if判断会把整形中的 0 识别为false(空)
❌
select * from org where ISNULL(parent_id)
✔️
查询varchar类型不为空Null的字段
select * from org where name <> ""
❌把空字符串和Null都过滤掉了
select * from org where name != ""
❌把空字符串和Null都过滤掉了
select * from org where !ISNULL(name)
✔️3这个其实是一个空字符串,结果是查询到了不为空的
查询varchar类型为空的字段
select * from org where name = ""
❌这是查询到了为空字符串的
select * from org where ISNULL(name)
✔️这是查询到了为空的
总结🌎
int类型用 ! isNull,varchar用 ! = “”
查询int类型
- 不为空 select * from org where !ISNULL(parent_id)
- 为空 select * from org where ISNULL(parent_id)
查询varchar类型
- 不为空 select * from org where !ISNULL(name)
- 不为空字符串 select * from org where name <> "" 或 select * from org where name != ""
- 为空 select * from org where ISNULL(name)
- 为空字符串 select * from org where name = ""
(一般判断字符串是否为空就是null和空字符串都判断,具体看自己需求)