1)创建表
CREATE TABLE ad_position_ad (
p_id bigint ,
p_name character varying(20) ,
devid integer ,
ad_type character varying(20) ,
platform character varying(20) ,
category_id integer ,
config character varying(20) ,
is_check character varying(20) ,
status character varying(20) ,
showing_total bigint ,
click_total bigint ,
user_total bigint ,
income_total bigint ,
online_time integer ,
create_time integer ,
modify_time integer);
创建表后,我们在表中插入一些数据。
2)表的数据
mydb=> select count(*) from ad_position_ad;
count
-------
275
(1 row)
mydb=> select distinct config from ad_position_ad;
config
--------
2
0
1
(3 rows)
3)将varchar列转换为smallint
mydb=>
alter table ad_position_ad
alter column config type smallint using platform::smallint;
ALTER TABLE
4)再次检查数据
mydb=> select distinct config from ad_position_ad;
config
--------
3
(1 row)
请注意,列配置的值已经更改。有人知道吗? 但是以下命令工作正常,值不会改变:
mydb=>
alter table ad_position_ad
alter column config type smallint using cast(config as smallint);
ALTER TABLE
答案 0 :(得分:3)
在重写问题后,我终于认为我找到了真正的问题。我引用:
alter table ad_position_ad
alter column config type smallint using platform::smallint;
您意识到自己是ALTER COLUMN
config
,但是使用 platform
列的值呢?
尝试:
alter table ad_position_ad
alter column config type smallint using config::smallint;