简体   繁体   English

根据多个条件,将记录从一个表中的特定列插入到另一表中

[英]inserting records from specific columns in one table to another table depending on multiple criteria

I'm Using SQL server and need to Inset records from one table into another empty table. 我正在使用SQL Server,需要将记录从一个表插入另一个空表。 the first table im pulling from is called accnt and has the fields: code, invno, invdate, ven, and amnt, and imported. 我从中提取的第一个表称为accnt,并具有以下字段:code,invno,invdate,ven和amnt,以及导入的。 the table that the records are going to is called quick and has the fields: date, num, name, account, split, and amount. 记录将转到的表称为快速表,具有以下字段:日期,数字,名称,科目,拆分和金额。 some of the fields are just directly copied which i understand, but what im having trouble with is that some of the fields need to be populated with a different field from accnt depending on the value in account. 我理解有些字段是直接复制的,但是我遇到的麻烦是,根据帐户中的值,某些字段需要使用accnt以外的其他字段填充。 and there is a field in accnt called imported that is either empty or has an x in it. accnt中有一个名为import的字段,该字段为空或带有x。 i only want to import records where the imported field is empty 我只想导入导入字段为空的记录

the insert statement i have so far should import: date, name, account, and amount directly not depending on the anything. 到目前为止,我拥有的插入语句应直接输入:日期,名称,科目和金额,而不取决于任何内容。 But the num field imports from a different field in accnt depending on the value of the accno field. 但是num字段根据accno字段的值从accnt中的其他字段导入。 the criteria is: 条件是:

if account < 7000 then num = code if account > 7000 then num = invno 如果帐户<7000,则num =代码如果帐户> 7000,则num = invno

here is what i tried, but it didnt work 这是我尝试过的,但是没有用

DELETE FROM quick
INSERT INTO quick (date, num, name, account, amount) 
if accnt.accno < '7000'
   then SELECT invdate, code, ven, accno, amnt from accnt
if accnt.accno > '7000'
   then SELECT invdate, invno, ven, accno, amnt from accnt

how do i accomplish what i'm trying to do? 我如何完成我想做的事情? the table looks like this even though the data in the picture is wrong: 即使图片中的数据有误,该表也会如下所示:

在此处输入图片说明

the type and split fields will be populated later with an update query. type和split字段稍后将使用更新查询进行填充。

Try it with a single SELECT and use CASE to separate the two situations. 使用单个SELECT尝试一下,并使用CASE来区分这两种情况。

DELETE FROM quick
INSERT INTO quick (date, num, name, account, amount) 
SELECT invdate,
case when accnt.accno < '7000' then code else invno end
, ven, accno, amnt from accnt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据特定条件从一个表中检索与另一个表相关的记录 - Retrieve records from one table related to another depending on certain criteria 按多列显示一个表中的记录,该表不在另一个表中 - Display records from one table which is not in another table by multiple columns Mysql查询在多个条件和列上从一个表插入到另一个表 - Mysql Query for inserting from one table to another on Multiple conditions and columns 如何将一个表的记录(特定的行,具有多个(但不是全部)列)附加到另一个表 - How can I append records (specific rows, with multiple (but not all) columns) from one table to another 使用另一个表中的PK在数据库表中插入多个记录 - Inserting multiple records in database table using PK from another table SQL将一个表的多条记录合并成另一表的多列 - SQL combine multiple records of one table into multiple columns of another table 如何从一个表中多列满足不同条件的记录中查找记录? - How to find records from one table where multiple columns meet different criteria? 将数据从另一表的一列插入到同一表的多列中 - Inserting data into multiple columns of same table from one column of another table SQL:将一个表中的所有记录插入到另一个表中,但没有指定列 - SQL: Insert all records from one table to another table without specific the columns 使用MySQL中另一个表的多个条件对不同的记录进行计数 - Counting Distinct Records Using Multiple Criteria From Another Table In MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM