背景
hive/mysql下使用sql中的between * and * 在不同數(shù)據(jù)類型下竟然不一樣崔拥。
1催什、int類型悔详、float類型镊屎、string類型是包含頭尾的。
2茄螃、timestamp包含頭缝驳,但不包含尾!9椴浴用狱!此外,當(dāng)between a and a 時(shí)拼弃, 是不包含a的夏伊。
具體原因,有待進(jìn)一步調(diào)研吻氧。
避坑辦法
使用大于等于和小于等于替代between * and * 溺忧,避免這個(gè)問題咏连。
驗(yàn)證過程
1、int鲁森、float類型數(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
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
2祟滴、string類型——包含頭尾
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
3、timestamp類型——包含頭歌溉,但不包含結(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