开发者

copying between multiple tables

开发者 https://www.devze.com 2023-03-25 14:12 出处:网络
I have some records in tables Awith fields i.e firstname surname,开发者_运维知识库 lastname, school, dob

I have some records in tables A with fields i.e firstname surname,开发者_运维知识库 lastname, school, dob

I have another table B with some records and fields i.e firstname, surname, address, club,

I'd like to use the firstname and surname in table B to check if the record exist in table A if it doesnt it should append the record to table A. I would be glad if you can help me with this


This SQL should do this. Basically, insert from table_b, records with firstname and lastname not existing in table_a.

INSERT INTO table_a (firstname, lastname, address, club)
(
  SELECT DISTINCT firstname, lastname, address, club FROM table_b 
  WHERE (firstname, lastname) NOT IN (SELECT firstname, lastname FROM table_a)
)


Try this query -

INSERT INTO table_a(firstname, lastname) 
  SELECT b.firstname, b.lastname FROM table_b b
    LEFT JOIN table_a a ON b.firstname = a.firstname AND b.lastname = a.lastname
  WHERE a.firstname IS NULL AND a.lastname IS NULL;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号