二月初三辛丑年 牛辛卯月 壬戌日
好多天沒更博了,為啥呢,因為我的需求上線了,然后又被bibibi了,其中各種心酸背鍋以及瑟瑟發抖。。。天吶
回來繼續說,今天一個sql的修改:
需求是這樣的:在一個日期范圍內(2020-03-01至2021-03-12)查詢人員類型為(“1003%”、“1004%”、“1006%”)的數據。
1、我原來錯誤的寫法為:
當我查詢出來的數據時間有2015年的,並且查詢時間特別特別慢
-- 這段腳本查詢出來的數據和時間沒有什么關系的
select m.no, m.callDate, m.personType from amain m where m.callDate >= date '202-03-01' and m.callDate< date'2021-03-12' and m.personType LIKE '1003%' or m.personType LIKE '1004%' or m.personType LIKE '1006%' ;
-- 即當出現:
-- condition1 and condition2 OR condition3
-- 其運算實際上是等價於:(condition1 and condition2) or condition3 //先運算and 再運算or
--運算順序為 and > or
2、正確的寫法為:
select m.no, m.callDate, m.personType from amain m where m.callDate >= date '202-03-01' and m.callDate< date'2021-03-12' and (m.personType LIKE '1003%' or m.personType LIKE '1004%' or m.personType LIKE '1006%' );
-- 加入了括號之后就能查出正確的數據了
-- condition1 and (condition2 OR condition3)
-- 其運算實際上先運行了 括號里面的 or 再運行了 括號外的 and
-- 運算順序為 () > and
綜合上述兩種情況,就可得到:運算順序為 () > and >or.