簡體   English   中英

如何編寫一個SQL修補程序,該修補程序將獲取表A中的值並將其作為行插入表B中?

[英]How to write a SQL patch that will take values from table A and insert as row within table B?

我只需要獲取photoProcess表中具有storyId值(不是0或null)的每一行,並將條目插入到名為photoProcessStory的新表中。

photoProcess表中的行示例:

+----------------+----------+----------+
|  processId     | storyId  |  rating  |
+----------------+----------+----------+
|  111111111     | 322      |  1       |
|  111111112     | 333      |  1       |
|  111111113     | 0        |  1       |
+----------------+----------+----------+

最終產生的photoProcessStory表如下所示:

+-----+----------------+-------------------+
| id  |  processId     | storyId           |
+-----|----------------+-------------------+
| 1   |  111111111     | 322               |
| 2   |  111111112     | 333               |
+-----+----------------+-------------------+

任何幫助將不勝感激。 我有連接表並將值插入列的經驗,但是對於如何在這樣的補丁中插入行並不熟悉。

insert into photoProcessStory (processId, storyId)
select processId, storyId
from photoProcess
where storyId <> 0 and storyId is not null

您可以使用INSERT INTO.. SELECT ,例如:

INSERT INTO photoProcessStory (processId, storyId)
SELECT processId, storyId FROM photoProcess WHERE storyId <> 0;

這只是一個insert . . . select insert . . . select insert . . . select

insert into photoProcessStory(processId, storyId)
    select processId, storyId
    from photoProcess
    where storyId <> 0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM