Mysql 如果值为空,如何跳过查询选择

Mysql 如果值为空,如何跳过查询选择,mysql,sql,spring,hibernate,Mysql,Sql,Spring,Hibernate,我想省略最后一个,其中“+”和(?11!=NULL和(s.client!=0或s.client!=NULL)或s.client.contactName,如%?11%)“,如果?11与NULL不同但不起作用 我的错误就是为什么我只想添加客户端atribute是一个新列,是一个外来列,但我有错误,因为我想在该列中获取null和0的旧数据 @Query("SELECT new com.cea.platforms.shipmentcontrol.dto.shipments.ShipmentFi

我想省略最后一个,其中“+”和(?11!=NULL和(s.client!=0或s.client!=NULL)或s.client.contactName,如%?11%)“,如果?11与NULL不同但不起作用

我的错误就是为什么我只想添加客户端atribute是一个新列,是一个外来列,但我有错误,因为我想在该列中获取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);
@Query(“选择new com.cea.platforms.shipmentcontrol.dto.shippings.ShipmentFilterDTO(”
+“s.shipmentId,”
+“s.shipmentStatus.catalogId”
+“s.shipmentStatus.catalogOptions”
+s.serviceType.catalogId
+s.serviceType.catalogOptions
+“s.trackingNumber,”
+“s.trasportUnitId,”
+“s.pkgReceptionDate,”
+“s.交货日期,”
+s.pkgDeliveryEstimateDate
+“s.register.catalogId,”
+“s.register.catalogOptions”
+“s.deliveryOrderSeq)来自ShipmentModel s”
+“哪里”
+“(?1=NULL或s.register.catalogId=?1)”
+“和(?2=NULL或s.trackingNumber,如%?2%)”
+“和(?3=NULL或s.serviceType.catalogId=?3)”
+“和(?4=NULL或s.trasportUnitId LIKE%?4%)”
+“和(?5=NULL或(强制转换(s.pkgReceptionDate作为日期)>=?5))”
+“和(?6=空或(强制转换(s.pkgReceptionDate作为日期)=?7))”

+和(?8=NULL或(CAST(s.deliveryDate AS date)首先,将任何内容与
NULL
进行比较都会产生
NULL
结果,而不管比较的实际值如何。通常,
NULL=NULL
NULL
,而
1null
也是
NULL

您需要将代码中的所有
..=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% 
)
和(
?11不为空
和(s.client 0或s.client不为空)
或s.client.contactName,如%?11%
)
这实际上意味着:

AND (
    ?11 IS NOT NULL 
    AND ((s.client <> 0 OR s.client IS NOT NULL) OR s.client.contactName LIKE %?11%) 
)
和(
?11不为空
和((s.client 0或s.client不为空)或s.client.contactName,如%?11%)
)
这是您想要的逻辑吗?否则,您可能需要用括号括住一些谓词。例如:

AND (
    (?11 IS NOT NULL AND (s.client <> 0 OR s.client IS NOT NULL))
    OR s.client.contactName LIKE %?11% 
)
和(
(?11不为空且(s.client 0或s.client不为空))
或s.client.contactName,如%?11%
)

谢谢,我会尝试的。我的错误是为什么我只想添加客户端atribute是一个新列,是一个外来列,但我有错误,因为我想在该列中获取空值和0的旧数据。我尝试了上一次查询,但在s.client.contactName中得到了有效id的结果。我需要在s.client.contact中得到无效id的结果名字也一样;