我正在尝试复制一组类似于以下内容的数据:
ADDRESS:
| ADDRESS_CODE | ADDRESS_TYPE | COLUMNCOLUMCOLUMN... (40 columns or so)
| DEFAULT | VENDOR1 | DATA DATA DATA
| 002 | VENDOR1 | DATA DATA DATA
| 003 | VENDOR1 | DATA DATA DATA
| 004 | VENDOR1 | DATA DATA DATA
| 005 | VENDOR1 | DATA DATA DATA
联系:
| CONTACT_CODE | VENDOR_ID | COLUMNCOLUMNCOLUMN... (30 columns or so)
| DEFAULT | VENDOR1 | DATA DATA DATA
| 002 | VENDOR1 | DATA DATA DATA
我正在尝试使用多个SQL查询来解决这个问题,但是它只会从联系人中复制Default和002 5次,或者只在无限循环中执行其中一行。我正在插入联系表。
这是我想要的CONTACT结果:
| CONTACT_CODE | VENDOR_ID | COLUMNCOLUMNCOLUMN... (30 columns or so)
| DEFAULT | VENDOR1 | DATA DATA DATA
| 002 | VENDOR1 | DATA DATA DATA
| 003 | VENDOR1 | DATA DATA DATA
| 004 | VENDOR1 | DATA DATA DATA
| 005 | VENDOR1 | DATA DATA DATA
任何帮助都将不胜感激。
我一直在使用以下查询:
select c.vendor_id, c.contact_code, a.address_code
from contact c, address a
inner join contact
on c.contact_code <> a.address_code
and c.vendor_id = a.address_type
group by c.vendor_id;
答案 0 :(得分:1)
insert into contact (CONTACT_CODE,VENDOR_ID,<COLUMNS list>)
select Address_code, Address_type, <COLUMNS list>
from ADDRESS a
left join contact c
on a.address_code=c.contact_code
and a.address_type=c.vendor_id
where c.contact_code is null
答案 1 :(得分:0)
您可以在地址和联系人表之间使用左外连接来获得所需的结果。
答案 2 :(得分:0)
您可以在条件中使用INSERT
语句:
INSERT INTO contact c
(contact_code, vendor_id, < OTHER COLUMNS >)
SELECT a.address_code, a.address_type, a.OTHER COLUMNS,
FROM address a
WHERE NOT EXISTS (SELECT *
FROM CONTACT c, address a
WHERE a.address_code = c.contact_code
AND a.address_type = c.vendor_id);
我没有测试过它..