Fetch Data of two tables and insert to new table

China☆狼群 提交于 2019-12-12 04:47:18

问题


I have three activities in my application with following names:
Activity #1
Activity #2
Activity Final

Also for each activity, I have a table with following names:
Table #1
Table #2
Table Final

Default value of Qty column is "0" and users will change it at run-time.

I've created Table#1 and Table#2 and they work fine.

I just don't know how can I add rows of Table#1 and Table#2 where "Qty" is bigger than "0" into Table Final.

Note: id column of each tables is PRIMARY KEY AUTOINCREMENT


回答1:


INSERT INTO "Table Final"(Product, Qty)
SELECT Product, Qty FROM "Table #1" WHERE Qty > 0
UNION ALL
SELECT Product, Qty FROM "Table #2" WHERE Qty > 0;



回答2:


You could use unionwordkey to achieve this.

INSERT INTO Final(Product, Qty)
(SELECT Product, Qty FROM t1 where Qty > 0)
UNION ALL
(SELECT Product, Qty FROM t2 where Qty > 0)

Hope it helps



来源:https://stackoverflow.com/questions/25947396/fetch-data-of-two-tables-and-insert-to-new-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!