M
M
Mikha Pankratov2015-11-29 23:27:44
MySQL
Mikha Pankratov, 2015-11-29 23:27:44

How to compare 2 tables and display an id that is not in the second table?

Hello, there are 2 tables

test1
id
name
test2
id
test_id
type
name_test2

I need to find out which test.id is not in test2.test_id, if there are, add
2 lines for each not found id in the first line type = 1 in the second type=2. Something doesn't work, thanks for the help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2015-11-29
@frmax

insert into test2 (test_id, type)
select test.id, type_value from test1 
join (
select 1 as type_value
union all
select 2
) as types ON TRUE -- нужно декартово произведение
where not exists (select 1 from test2 where test2.test_id = test.id)

If mysql is ancient, then it might be more efficient to test left join test2 on test2.test_id = test.id where test2.test_id is null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question