SQL查找非空元素(!=NULL)

本文介绍了一种常见的SQL查询错误,即使用“address!=null”进行查询可能会导致没有可用选项的问题,并给出了正确的写法:应该使用“address IS NOT NULL”来避免这类情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例如:
select name from clase where address != null

这样的话查询语句不会报错,但是永远不会有可用的选项

SQL中要想实现这个功能有另外一个表达的方式

select name from clase where address IS NOT NULL
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.kmbeast.mapper.UserHealthMapper"> <insert id="batchSave"> INSERT INTO userhealth (user_id, health_model_id, value, create_time) VALUES <foreach collection="list" item="userHealth" index="index" separator=","> (#{userHealth.userId},#{userHealth.healthModelId},#{userHealth.value},#{userHealth.createTime}) </foreach> </insert> <update id="update"> UPDATE userhealth <set> <if test="value != null and value != ''"> value = #{value}, </if> </set> WHERE id = #{id} </update> <delete id="batchDelete" parameterType="list"> DELETE FROM userhealth WHERE id IN <foreach collection="ids" item="id" open="(" separator="," close=")"> #{id} </foreach> </delete> <select id="query" resultMap="BaseResultMap"> SELECT uh.*,u.user_name,hmc.name,hmc.unit,hmc.symbol,hmc.value_range FROM userhealth uh LEFT JOIN user u ON u.id = uh.user_id LEFT JOIN health_model hmc ON hmc.id = uh.health_model_id <where> <if test="userId != null"> AND uh.user_id = #{userId} </if> <if test="healthModelId != null"> AND uh.health_model_id = #{healthModelId} </if> <if test="startTime != null and endTime != null"> AND uh.create_time BETWEEN #{startTime} AND #{endTime} </if> </where> ORDER BY uh.create_time DESC <if test="current != null and size != null"> LIMIT #{current},#{size} </if> </select> <select id="queryCount" resultType="integer"> SELECT COUNT(*) FROM user_health uh <where> <if test="userId != null"> AND uh.user_id = #{userId} </if> <if test="healthModelId != null"> AND uh.health_model_id = #{healthModelId} </if> <if test="startTime != null and endTime != null"> AND uh.create_time BETWEEN #{startTime} AND #{endTime} </if> </where> </select> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="cn.kmbeast.pojo.vo.UserHealthVO"> <id column="id" property="id"/> <result column="user_id" property="userId"/> <result column="user_name" property="userName"/> <result column="health_model_id" property="healthModelId"/> <result column="value" property="value"/> <result column="create_time" property="createTime"/> <result column="name" property="name"/> <result column="unit" property="unit"/> <result column="sign" property="sign"/> <result column="value_range" property="valueRange"/> </resultMap> </mapper>
06-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值