插入表中,从表a和表b中选择 [英] Insert into table select from table a and table b where

查看:61
本文介绍了插入表中,从表a和表b中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将两个表(每个ID都匹配的where条件)连接起来,将数据插入到表中.

I want to insert data into a table by joining two tables with a where condition that each id matches.

INSERT INTO SALES(T_ID, SF)
SELECT B.T_ID, B.SF
FROM HIS B, SALES C
WHERE C.REP_ID=B.REP_ID;

我收到一个错误,我无法将NULL插入("c.REP_ID")

I am getting an error that I cannot insert NULL into ("c.REP_ID")

我不打算在c.rep_id中插入任何内容.我想从HIS表中将值插入t_id,sf,其中他表上的rep_id =销售表上的rep id.

I am not trying to insert anything into c.rep_id. I want to insert values into t_id, sf from HIS table where the rep_id on his table = rep id on sales table.

推荐答案

INSERT INTO SALES (T_ID, SF)
SELECT h.T_ID, h.SF FROM HIS h, SALES C
WHERE C.REP_ID=B.REP_ID;

注意:请确保来自选择表的数据与要插入的列的数据类型具有同等的比较性和有效性.

Note: Make sure that the data comes from select tables is equally compare and valid with the datatypes of the columns where you inserting.

这篇关于插入表中,从表a和表b中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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