【HIVE/MySQL】 sql中 between * and * 在不同數(shù)據(jù)類(lèi)型下的差異

背景

hive/mysql下使用sql中的between * and * 在不同數(shù)據(jù)類(lèi)型下竟然不一樣。
1、int類(lèi)型、float類(lèi)型、string類(lèi)型是包含頭尾的。
2、timestamp包含頭,但不包含尾?。?!此外,當(dāng)between a and a 時(shí), 是不包含a的。
具體原因,有待進(jìn)一步調(diào)研。

避坑辦法

使用大于等于和小于等于替代between * and * ,避免這個(gè)問(wèn)題。

驗(yàn)證過(guò)程

1、int、float類(lèi)型數(shù)據(jù)——包含頭尾

select
    7 as time
    , case when 7  between 7 and 8 then "1" else "0" end as judge
union all
select
    8 as time
    , case when 8  between 7 and 8 then "1" else "0" end as judge
圖片.png
select
    7.0  as time
    , case when 7.0   between 7.0  and 8.0  then "1" else "0" end as judge
union all
select
    8.0  as time
    , case when 8.0   between 7.0  and 8.0  then "1" else "0" end as judge
圖片.png

2、string類(lèi)型——包含頭尾

select
    to_date(now())  as time
    , case when to_date(now())              between "2020-11-11" and "2020-11-12" then "1" else "0" end as judge
union all
select
    to_date(date_add(now(),1)) as time
    , case when to_date(date_add(now(),1))  between "2020-11-11" and "2020-11-12" then "1" else "0" end as judge
圖片.png

3、timestamp類(lèi)型——包含頭,但不包含結(jié)尾

select
    now() as time
    , case when now()               between "2020-11-11" and "2020-11-12" then "1" else "0" end as judge
union all
select
    date_add(now(),1) as time
    , case when date_add(now(),1)  between "2020-11-11" and "2020-11-12" then "1" else "0" end as judge
圖片.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容