sql 语句 c.id is null 与 id = ''

本文讨论了在使用SQL进行数据查询时遇到的问题,即SQL语句正确但查询结果不完整的情况。通过逐步拆分和简化SQL语句,最终发现数据中存在isnull与''的区别导致查询遗漏部分数据。文章提供了修改数据库中'脏数据'的解决方案。

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

许久没有写过SQL语句了,连关键词也都忘的一干二净了。


今天做项目 (Heal Your Body)的时候,发现数据显示与iPhone那边显示的不一致。详细检查了自己的sql ,发现sql书写完全正确,不知道为什么query的时候,漏了两条数据。


通过肉眼观察,这两种数据完全满足sql里面的条件语句。不知道为什么一直就是查不出来。


完整的sql语句:

select a.id, (select b.problem from bookdata as b where b.id=a.subidof) || ' ' || a.problem as problem from bookdata as a where a.subidof <> '' union all select c.id, c.problem as problem from bookdata as c where c.subidof is null and (c.Cause is not null or c.Cure is not null or c.seeNext is not null ) order by problem asc;


通过拆分sql语句后意外发现:

select c.id, c.problem as problem from bookdata as c where c.subidof is null and (c.Cause is not null or c.Cure is not null or c.seeNext is not null ) order by problem asc;

此句执行的时候,也查不出那两条数据。

再简化:

select c.id, c.problem as problem from bookdata as c where c.subidof is null ;


发现,还是会漏掉两条数据。 ---- (第65,68条)


再通过 :

select c.id, c.problem as problem from bookdata as c where c.subidof = '';

查出仅的那两种漏掉的数据。


原来你通过肉眼看到(SQLite Database browser 或者 Sqlite Administer工具 )的某些字段是空的,其实不一定是空的,它也许 is null,也许是 ‘’


soluton:修改数据库中的‘脏数据’







转换为 驼峰语句SELECT * FROM OmsOrder ord LEFT JOIN BaseOrderType orderType ON orderType.id = ord.orderTypeId LEFT JOIN BaseServiceProduct productId ON productId.id = ord.productId LEFT JOIN OmsOrder originalOrderId ON originalOrderId.id = ord.originalOrderId LEFT JOIN BaseVehicleType reqVehicleTypeId ON reqVehicleTypeId.id = ord.reqVehicleTypeId LEFT JOIN BaseLocation predepLocationId ON predepLocationId.id = ord.predepLocationId LEFT JOIN BaseLocation dpLocationId ON dpLocationId.id = ord.dpLocationId LEFT JOIN BaseLocation dispatchPlaceSend ON dispatchPlaceSend.id = dpLocationId.dpLocationId LEFT JOIN BaseLocation consignorAddress ON consignorAddress.id = ord.consignorAddress.regId LEFT JOIN BaseLocation arrivalLocationId ON arrivalLocationId.id = ord.arrivalLocationId LEFT JOIN BaseLocation dispatchPlaceReceive ON dispatchPlaceReceive.id = arrivalLocationId.dpLocationId LEFT JOIN BaseLocation consigneeAddress ON consigneeAddress.id = ord.consigneeAddress.regId LEFT JOIN BaseCustomer customerId ON customerId.id = ord.customerId LEFT JOIN BaseProvider carrierId ON carrierId.id = ord.carrierId LEFT JOIN ThornBaseOrganization executeOrg ON executeOrg.id = ord.executeOrgId LEFT JOIN TmsDispatchOrder dispatch ON dispatch.id = { maintainTransportTmsDispatchOrderPage.id } WHERE 1 = 1 AND ord.beHalfDayDelivery = FALSE AND dispatch.transportType = ord.transportType AND date_format(dispatch.actualLoadingSTime, '%Y%m%d') = date_format(ord.estimatedDepartureTime, '%Y%m%d') AND ( ( ((ord.STATUS = 'A' AND (dispatch.inquiryCode IS NULL OR dispatch.inquiryCode = ''))) OR ( ( ord.STATUS = 'WB' AND dispatch.carrierId = ord.carrierId AND dispatch.twoCarrierId = ord.twoCarrierId AND dispatch.threeCarrierId = ord.threeCarrierId AND dispatch.inquiryCode = ord.inquiryCode ) ) ) ) AND ord.executeOrgId = 1026 AND ord.STATUS IN ('WB', 'A') AND ord.beLocked = FALSE AND ord.beToExecute = TRUE ORDER BY ord.updateInfo.createdTime, consignorAddress.NAME, consigneeAddress.NAME
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值