繁体   English   中英

如果值为 null,如何跳过查询 select

[英]How can skip a query select if the value is null

我想省略最后一个 where ' + "AND (?11.=NULL AND (s.client.=0 OR s.client?=NULL) OR s?client.contactName LIKE %?11% ) "' if ?11与 null 不同,但不工作

我的错误是为什么我只是添加客户端属性是一个新列并且是外部列但我遇到错误因为我想在该列中使用 null 和 0 获取旧数据

@Query("SELECT new com.cea.platforms.shipmentcontrol.dto.shipments.ShipmentFilterDTO("
        + "s.shipmentId,"
        + "s.shipmentStatus.catalogId,"
        + "s.shipmentStatus.catalogOptions,"
        + "s.serviceType.catalogId,"
        + "s.serviceType.catalogOptions,"
        + "s.trackingNumber,"
        + "s.trasportUnitId,"
        + "s.pkgReceptionDate,"
        + "s.deliveryDate, "
        + "s.pkgDeliveryEstimateDate, "
        + "s.register.catalogId,"
        + "s.register.catalogOptions,"
        + "s.deliveryOrderSeq) FROM ShipmentModel s " 
        + "WHERE "
        + "(?1 = NULL OR s.register.catalogId = ?1) "
        + "AND (?2 = NULL OR s.trackingNumber LIKE %?2%) "      
        + "AND (?3 = NULL OR s.serviceType.catalogId = ?3) "        
        + "AND (?4 = NULL OR s.trasportUnitId LIKE %?4%) "
        + "AND (?5 = NULL OR (CAST(s.pkgReceptionDate AS date) >= ?5)) "
        + "AND (?6 = NULL OR (CAST(s.pkgReceptionDate AS date) <= ?6)) "    
        + "AND (?7 = NULL OR (CAST(s.deliveryDate AS date) >= ?7)) "
        + "AND (?8 = NULL OR (CAST(s.deliveryDate AS date) <= ?8)) "            
        + "AND (?9 = NULL OR  s.shipmentStatus = ?9) " 
        + "AND (?10  = NULL OR s.shipmentReference IS NULL OR s.shipmentReference LIKE %?10%) "
        + "AND (?11 IS NULL OR s.client=0 OR s.client!=0 ) "
        + "AND (?11!=NULL AND (s.client!=0 OR s.client!=NULL) OR s.client.contactName LIKE %?11% ) "

        )
Page<ShipmentFilterDTO> findByFilters(Long companyId, String trackingNumber, Long serviceTypeId,
        String transportUnitId, Date receptionDate, Date receptionDateEnd, Date deliveryDate, Date deliveryDateEnd,
        Long shipmentStatusId,String reference,String ClientName, Pageable pageable);

首先,将任何内容与null进行比较会产生null结果,无论所比较的实际值如何。 Typically, NULL = NULL is NULL , and 1 <> NULL is NULL as well .

您需要将代码中的所有... = NULL谓词替换为... IS NULL 因此, ... != NULL应该是... IS NOT NULL

我也怀疑最后一个条件中谓词的优先级:

AND (
    ?11 IS NOT NULL 
    AND (s.client <> 0 OR s.client IS NOT NULL) 
    OR s.client.contactName LIKE %?11% 
)

这实际上意味着:

AND (
    ?11 IS NOT NULL 
    AND ((s.client <> 0 OR s.client IS NOT NULL) OR s.client.contactName LIKE %?11%) 
)

这是你想要的逻辑吗? 否则,您可能需要用括号括起一些谓词。 例如:

AND (
    (?11 IS NOT NULL AND (s.client <> 0 OR s.client IS NOT NULL))
    OR s.client.contactName LIKE %?11% 
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM