如何将数据从一个表插入另一个表 [英] how to insert the data from one table to another table

查看:156
本文介绍了如何将数据从一个表插入另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用字段上的where子句将table2中的行插入table1



例如。



table1 columns



date voucherno cashbalance bankbalance narration







table2栏



约会voucherno总账余额旁白





i希望在日期插入日期,voucherno在voucherno,如果ledgerhead名称是现金然后将余额插入到Cashbalance否则0,如果ledgerhead名称是bank然后将余额插入到bankbalance其他0,并且叙述从表格旁白表2到表1。



答案赞赏......

解决方案

你可以用 SQL INSERT INTO SELECT [ ^ ]声明

示例:

  INSERT   INTO 客户(客户名称,国家/地区)
SELECT SupplierName,Country < span class =code-keyword> FROM 供应商
WHERE Country = ' 德国';


您可以尝试:

< pre lang =SQL> INSERT INTO table1( date ,voucherno,cashbalance,bankbalance,narration)
SELECT date ,voucherno
案例 ' cash' 然后 balance else 0 end as cashbalance
案例 ledgerhead ' bank' 然后 balance else 0 end as bankbalance
,旁白 FROM table2


How to insert the rows from table2 to table1 using where clause on field

eg.

table1 columns

date voucherno cashbalance bankbalance narration



table2 columns

date voucherno ledgerhead balance narration


i would like to insert the date on date, voucherno on voucherno, if ledgerhead name is cash then insert the balance to cashbalance else 0,if ledgerhead name is bank then insert the balance to bankbalance else 0, and narration to narration from table 2 to table1.

Answers appreciated......

解决方案

you can use SQL INSERT INTO SELECT [^]statement
Example:

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';


You may try with:

INSERT INTO table1 (date, voucherno, cashbalance, bankbalance, narration)
SELECT date,voucherno
,Case ledgerhead when 'cash' then balance else 0 end as cashbalance
,Case ledgerhead when 'bank' then balance else 0 end as bankbalance
,narration FROM table2


这篇关于如何将数据从一个表插入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆