mysql开发小笔记
1. sql执行完毕后发现sql查询语句查询出的结果远远超出了自己预期的范围,
原语句是:select xx from xx where a=b and c=d or e=f;
预期的结果是满足a=b的同时c=d或者e=f,
原因:忘记加括号导致范围超限
解决:给or连接的多个条件外面加上括号即可:
select xx from xx where a=b and(c=d or e=f);
总结:当多个条件写在一起进行筛选过滤查询的时候,也就是当执行sql时,根据筛选条件只想让它按照其中的某一个对应的条件去执行,其中为了防止两个或多个条件之间有"瓜葛"查询出许多多余的不对应的我们不需要的数据,也就是范围超限,我们可以在这两个或多个条件外面套一个"( )",这样就相当于将这些诸多条件看做是一个整体,根据查询条件进行筛选,最终只会有对应的一个条件,这样就防止了由于多个 "or" 连接的条件查询导致的范围超限问题。
例子:将 1 和 2 这两个sql组合到一起写,减少代码量提高工作效率,就可以按照 3 的写法写。
-- 1.太子城
@Query(value = "SELECT * FROM rec_monitor_device m WHERE m.station_code = :stationCode and device_category_code='0817' ",nativeQuery = true)
List<MonitorDevice> findByServiceStatus(@Param("stationCode") String stationCode, Pageable pageable);
-- 2.清河
@Query(value = "SELECT * FROM rec_monitor_device m WHERE m.station_code = :stationCode and device_category_code='0830' ",nativeQuery = true)
List<MonitorDevice> findByServiceStatus(@Param("stationCode") String stationCode, Pageable pageable);
-- 3.组合
@Query(value = "SELECT * FROM rec_monitor_device m WHERE m.station_code = :stationCode and (device_category_code ='0817' or device_category_code='0830') ,nativeQuery = true)
List<MonitorDevice> findByServiceStatus(@Param("stationCode") String stationCode, Pageable pageable);
=============================================================================
2.

浙公网安备 33010602011771号