我有這個查詢并向 MYSQl 資料庫插入行并且作業完美。
insert int test(id,user)
select null,user from table2
union
select null,user from table3
但是當在 PostgreSQL 中運行上述查詢時不起作用。我得到這個錯誤column "id" is of type integer but expression is of type text,但是當我運行下面的兩個查詢時,如圖所示。
當我在 PostgreSQL 中運行以下查詢時,它可以正常作業:
insert into test(id,user)
select null,user from table2
或者在 PostgreSQL 中的查詢下面它可以正常作業:
insert int test(id,user)
select null,user from table3
或者在 PostgreSQL 中的查詢下面它可以正常作業:
select null,user from table2
union
select null,user from table3
uj5u.com熱心網友回復:
null不是真正的值,因此沒有資料型別。默認假定的資料型別是text,這是錯誤訊息的來源。只需int在第一個 SELECT中將值轉換為:
insert into test(id, "user")
select null::int, "user" from table2
union
select null, "user" from table3
或者甚至更好,id完全省略,以便id使用為列定義的任何默認值。嘗試插入null到名為的列中聽起來很奇怪id
insert into test("user")
select "user" from table2
union
select "user" from table3
請注意,這user是一個保留關鍵字和內置函式,因此您必須參考它以避免出現問題。從長遠來看,我建議為該列找到一個不同的名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/403733.html
標籤:
