@Query("SELECT new com.cmict.iot.server.dao.model.sql.DeviceTransferInfoEntity(dts,dp.deptName,d.deviceName,d.name,a.booleanValue) " + "FROM DeviceTransferEntity dts " + "LEFT JOIN DeptEntity dp on dp.orgCode = dts.newOrgCode " + "LEFT JOIN DeviceEntity d on d.id = dts.deviceId " + "LEFT JOIN AttributeKvEntity a on a.id.entityId = d.id AND a.id.entityType = 'DEVICE' AND a.id.attributeType = 'SERVER_SCOPE' AND a.id.attributeKey = 'active' " + "WHERE dts.tenantId= :tenantId and dts.oldOrgCode= :orgCode ") Page<DeviceTransferInfoEntity> getDeviceTransferInfoByTenantIdAndOrgCode(@Param("tenantId") UUID tenantId, @Param("orgCode") String orgCode, Pageable pageable); 帮我修改sql,目前deviceName,deviceProfileName,active值为null
时间: 2024-02-10 17:12:22 浏览: 87
可以尝试修改以下的 SQL 查询语句,使用 `LEFT JOIN` 连接 DeviceProfileEntity 表,并将 `AttributeKvEntity` 表的连接方式修改为 `LEFT JOIN`,同时在查询结果中将 `DeviceProfileEntity` 表和 `AttributeKvEntity` 表的相关字段设置为 `COALESCE` 函数,以防止出现 `NULL` 值:
```
SELECT new com.cmict.iot.server.dao.model.sql.DeviceTransferInfoEntity(dts,dp.deptName,COALESCE(d.deviceName, ''),COALESCE(dp.name, ''), COALESCE(a.booleanValue, false))
FROM DeviceTransferEntity dts
LEFT JOIN DeptEntity dp on dp.orgCode = dts.newOrgCode
LEFT JOIN DeviceEntity d on d.id = dts.deviceId
LEFT JOIN DeviceProfileEntity dpf on dpf.id = d.deviceProfileId
LEFT JOIN AttributeKvEntity a on a.id.entityId = d.id AND a.id.entityType = 'DEVICE' AND a.id.attributeType = 'SERVER_SCOPE' AND a.id.attributeKey = 'active'
LEFT JOIN AttributeKvEntity ap on ap.id.entityId = dpf.id AND ap.id.entityType = 'DEVICE_PROFILE' AND ap.id.attributeType = 'SERVER_SCOPE' AND ap.id.attributeKey = 'active'
WHERE dts.tenantId = :tenantId and dts.oldOrgCode = :orgCode
```
这样可以在查询结果中将 `deviceName` 和 `deviceProfileName` 的 `NULL` 值替换为 `''` 空字符串,将 `active` 的 `NULL` 值替换为 `false` 值。
阅读全文