【hive】WHERE column_name IN (value1,value2,...),value字段是否要加单引号的情况分析


一、提出疑问

IN 操作语句

SELECT column_name(s) FROM table_name WHERE column_name IN
(value1,value2,…);

疑问:column_name的数据类型是否要与value1、value2数据类型一致??in括号里面的value1、value2…是否要加单引号,有什么区别??

二、情况分析

in的单字段匹配

举例:如果column_name类型是bigInt、double或者string,in()括号里面的值加不加单引号,都可以匹配成功。

> create table test_table1
(action_id bigint,
value string);
> insert overwrite table test_table1 values ('2003140','12'),('2003160','23'),('14027','34');
> select * from test_table1;
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
| 14027                  | 34                 |
+------------------------+--------------------+

## action_id是bigint类型,in()括号里面的值加单引号。
> select * from test_table1 where action_id in ('2003140','2003160','14027');
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
| 14027                  | 34                 |
+------------------------+--------------------+
## action_id是bigint类型,in()括号里面的值不加单引号。
> select * from test_table1 where action_id in (2003140,2003160,14027);
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
| 14027                  | 34                 |
+------------------------+--------------------+
> create table test_table2
(action_id double,
value string);
> insert overwrite table test_table2 values ('2003140','12'),('2003160','23'),('14027','34');
> select * from test_table2;
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
| 2003140.0              | 12                 |
| 2003160.0              | 23                 |
| 14027.0                | 34                 |
+------------------------+--------------------+

## action_id是double类型,in()括号里面的值加单引号。
> select * from test_table2 where action_id in ('2003140','2003160','14027');
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
| 2003140.0              | 12                 |
| 2003160.0              | 23                 |
| 14027.0                | 34                 |
+------------------------+--------------------+
## action_id是double类型,in()括号里面的值不加单引号。
> select * from test_table2 where action_id in (2003140,2003160,14027);
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
| 2003140.0              | 12                 |
| 2003160.0              | 23                 |
| 14027.0                | 34                 |
+------------------------+--------------------+
> create table test_table3
(action_id string,
value string);
> insert overwrite table test_table3 values ('2003140','12'),('2003160','23'),('14027','34');
> select * from test_table3;
+------------------------+--------------------+
| test_table3.action_id  | test_table3.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
| 14027                  | 34                 |
+------------------------+--------------------+

## action_id是string类型,in()括号里面的值加单引号。
> select * from test_table3 where action_id in ('2003140','2003160','14027');
+------------------------+--------------------+
| test_table3.action_id  | test_table3.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
| 14027                  | 34                 |
+------------------------+--------------------+
## action_id是string类型,in()括号里面的值不加单引号。
> select * from test_table3 where action_id in (2003140,2003160,14027);
+------------------------+--------------------+
| test_table3.action_id  | test_table3.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
| 14027                  | 34                 |
+------------------------+--------------------+

总结:单字段匹配,in括号里面的值加不加单引号都可以匹配上。

in多字段匹配

多个字段匹配一种情况

举例1:action_id是bigint类型,value是string类型

# 保持原有类型
> select * from test_table1 where (action_id,value) in ((2003140,12));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
+------------------------+--------------------+
> select * from test_table1 where (action_id,value) in (('2003140','12'));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
+------------------------+--------------------+


## 两者同时为string
> select * from test_table1 where (cast(action_id as string),value) in ((2003140,12));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table1 where (cast(action_id as string),value) in (('2003140','12'));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
+------------------------+--------------------+


## 两者同时为bigint
> select * from test_table1 where (action_id ,cast(value as bigint)) in ((2003140,12));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table1 where (action_id ,cast(value as bigint)) in (('2003140','12'));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
+------------------------+--------------------+

结论:无论是action_id与value是分别为bigint,string或者同时为bigint或者同时为string类型,in括号里面必须加单引号才能匹配成功。不考虑in括号里面一个带引号,一个不带引号。

举例2:action_id是double类型,value是string类型

# 保持原有类型
> select * from test_table2 where (action_id,value) in ((2003140,12));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id,value) in ((2003140.0,12));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id,value) in (('2003140','12'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id,value) in (('2003140.0','12'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
| 2003140.0              | 12                 |
+------------------------+--------------------+


# 两者同时为string
> select * from test_table2 where (cast(action_id as string),value) in ((2003140,12));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (cast(action_id as string),value) in ((2003140.0,12));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (cast(action_id as string),value) in (('2003140','12'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (cast(action_id as string),value) in (('2003140.0','12'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
| 2003140.0              | 12                 |
+------------------------+--------------------+


# 两者同时为double
> select * from test_table2 where (action_id ,cast(value as double)) in ((2003140,12));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id ,cast(value as double)) in ((2003140.0,12));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id ,cast(value as double)) in (('2003140','12'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id ,cast(value as double)) in (('2003140.0','12'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

结论
1、注意:double类型字段显示为2003140.0,因些(2003140,12)与(‘2003140’,‘12’)都无法匹配成功,要带单引号的(‘2003140.0’,‘12’)才能匹配成功,在单个字段匹配里面无所谓"2003140"与"2003140.0"。
2、无论是action_id与value是分别为double,string或者同时为string类型,in括号里面必须加单引号才能匹配成功,同时为double类型,无论加不加引号都匹配不成功。不考虑in括号里面一个带引号,一个不带引号。

举例3:action_id是string类型,value是string类型

> select * from test_table3 where (action_id,value) in ((2003140,12));
+------------------------+--------------------+
| test_table3.action_id  | test_table3.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table3 where (action_id,value) in (('2003140','12'));
+------------------------+--------------------+
| test_table3.action_id  | test_table3.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
+------------------------+--------------------+

结论:action_id与value同时为string类型,in括号里面必须加单引号才能匹配成功。不考虑in括号里面一个带引号,一个不带引号。

多个字段匹配多种情况
举例1:action_id是bigint类型,value是string类型

## 保持原有类型
> select * from test_table1 where (action_id,value) in ((2003140,12),(2003160,23));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table1 where (action_id,value) in (('2003140','12'),('2003160','23'));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
+------------------------+--------------------+

## 两者转换成string
> select * from test_table1 where (cast(action_id as string),value) in ((2003140,12),(2003160,23));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table1 where (cast(action_id as string),value) in (('2003140','12'),('2003160','23'));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
+------------------------+--------------------+

## 两者转换成bigint
> select * from test_table1 where (action_id ,cast(value as bigint)) in ((2003140,12),(2003160,23));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table1 where (action_id ,cast(value as bigint)) in (('2003140','12'),('2003160','23'));
+------------------------+--------------------+
| test_table1.action_id  | test_table1.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
+------------------------+--------------------+

结论:无论是action_id与value是分别为bigint,string或者同时为bigint或者同时为string类型,in括号里面必须加单引号才能匹配成功。不考虑in括号里面一个带引号,一个不带引号。

举例2:action_id是double类型,value是string类型

## 保持原有类型
> select * from test_table2 where (action_id,value) in ((2003140,12),(2003160,23));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id,value) in (('2003140','12'),('2003160','23'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id,value) in ((2003140.0,12),(2003160.0,23));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id,value) in (('2003140.0','12'),('2003160.0','23'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
| 2003140.0              | 12                 |
| 2003160.0              | 23                 |
+------------------------+--------------------+

## 两者转换成string
> select * from test_table2 where (cast(action_id as string),value) in ((2003140,12),(2003160,23));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (cast(action_id as string),value) in (('2003140','12'),('2003160','23'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+


> select * from test_table2 where (cast(action_id as string),value) in  ((2003140.0,12),(2003160.0,23));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (cast(action_id as string),value) in  (('2003140.0','12'),('2003160.0','23'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
| 2003140.0              | 12                 |
| 2003160.0              | 23                 |
+------------------------+--------------------+

## 两者转换成double
> select * from test_table2 where (action_id ,cast(value as double)) in ((2003140,12),(2003160,23));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id ,cast(value as double)) in (('2003140','12'),('2003160','23'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+
> select * from test_table2 where (action_id ,cast(value as double)) in ((2003140.0,12),(2003160.0,23));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table2 where (action_id ,cast(value as double)) in (('2003140.0','12'),('2003160.0','23'));
+------------------------+--------------------+
| test_table2.action_id  | test_table2.value  |
+------------------------+--------------------+
+------------------------+--------------------+

结论
1、注意:double类型字段显示为"2003140.0",“2003160.0”,因些(2003140,12)与(‘2003140’,‘12’),(2003160,23)与(‘2003160’,‘23’)都无法匹配成功,要带单引号的(‘2003140.0’,‘12’)与(‘2003160.0’,‘23’)才能匹配成功,在单个字段匹配里面无所谓"2003140"与"2003140.0"。
2、无论是action_id与value是分别为double,string或者同时为string类型,in括号里面必须加单引号才能匹配成功,同时为double类型,无论加不加引号都匹配不成功。不考虑in括号里面一个带引号,一个不带引号。

举例3:action_id是string类型,value是string类型

> select * from test_table3 where (action_id,value) in ((2003140,12),(2003160,23));
+------------------------+--------------------+
| test_table3.action_id  | test_table3.value  |
+------------------------+--------------------+
+------------------------+--------------------+

> select * from test_table3 where (action_id,value) in (('2003140','12'),('2003160','23'));
+------------------------+--------------------+
| test_table3.action_id  | test_table3.value  |
+------------------------+--------------------+
| 2003140                | 12                 |
| 2003160                | 23                 |
+------------------------+--------------------+

结论:action_id与value同时为string类型,in括号里面必须加单引号才能匹配成功。不考虑in括号里面一个带引号,一个不带引号。


三、总结

1、单个字段匹配,与数据类型无关,in括号里面字段加不加单引号进行匹配都行,都可以匹配成功。
2、多字段匹配,in括号里面字段必须要加单引号进行匹配,否则匹配不上,为空。
3、还要特别注意double类型,double类型就算两个字段都转换成string类型,匹配in括号里面加单引号字段也无法匹配;但其他数据类型,如bigint,转换成string格式,匹配in括号里面加单引号字段可以匹配成功。

猜你喜欢

转载自blog.csdn.net/sodaloveer/article/details/130593375