[英]Transfer data from one table to another along with other values (SQL)
我想將多個表中的數據以及一些默認值插入到一個表中。
假設我有3張桌子:
**table1**(col1, col2, col3, col4)
**table2**(col5, col6)
**table3**(col7)
我想要
insert in table1.col1 -> table2.col5,
table1.col2 -> table2.col6,
table1.col3 -> table3.col7,
table1.col4 -> 'default_value' for some WHERE condition.
我嘗試使用INSERT INTO...SELECT FROM
但是只有當**table1**
所有值均取自其他表而不是**"default_value"**.
時, **table1**
允許我輸入值**"default_value"**.
我不能使用2個不同的查詢,因為table1
所有列都具有NOT NULL
約束。
誰能幫我解決這個問題?
您尚未顯示嘗試過的查詢,它可以幫助那些試圖幫助您避免建議您嘗試過的內容的人。 但是,盡管不知道您嘗試了什么,但這應該可以工作-對我來說確實可以。
INSERT INTO table1 (col1, col2, col3, col4)
SELECT t2.first_name, t2.age, t3.salary, 'DEFAULT VALUE'
FROM table2 t2
INNER JOIN
table3 t3
ON t2.rec_id = t3.rec_id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.