Table1
id
Table2
id
Table3
id|table1_id|table2_id
I need the SQL (using MySQL) statement for:
for each row in table1 {
for each row in table2 {
insert in table3 values table1.id, table2.id;
}
Does an insert/select statement exists ? Or do I have to use a loop ?
You don't need to use loops at all. You should be thinking in terms of SETS when using a relational database.
Here is the correct way to achieve this. First using the CROSS JOIN to create a cartesian set of all the combinations of ids from table1 and table2. Then inserting that entire set into table3
Note: I am assuming Table3 ID is an auto-number.
INSERT INTO TABLE3 (TABLE1_ID,TABLE2_ID)
SELECT T1.ID
,T2.ID
FROM TABLE1 T1
CROSS JOIN
TABLE2 T2
Related
I have two tables with similar column values for columns "name" and "regis". One table is already filled with values, the other has only values for "name". How do i get "regis" values from table1 into table2 based on same column values?
I tried something like this
INSERT INTO table2 (regis)
SELECT table1 (regis)
WHERE table2.name = table1.name
Please try:
update table2 set regis = (select regis from table1 where table1.name=table2.name);
see the whole sample on:
https://www.db-fiddle.com/f/3jC8PGeZZEuty3XVq8gdzz/9
I think, what you are after is insert when name not found in table2 or update if its found in both tables.
INSERT INTO TABLE2 (regis)
SELECT t1.REGIS FROM TABLE1 as t1
LEFT OUTER JOIN TABLE2 as t2 on t2.name = t1.name
WHERE t2.name is null
You can update table2 using the statement like below:
update t2
set t2.regis = t1.regis
from table2 as t2
inner join table1 as t1 on t1.name = t2.name
If you are using SQL Server, then you can use merge statement to either insert or update in one step.
You need to use table2 as join in your select statement.
INSERT INTO table2 (regis)
SELECT table1.regis FROM Table1 INNER JOIN Table2 on table1.name = table2.name
How to select data from the table not exist in another table sql. I've tried NOT IN and NOT EXIST methods. But it causes performance issues for large amount of data. Can anyone suggest a solution for this.?
Thanks in advance.
I’ve tried the following.
SELECT name
FROM table1
WHERE NOT EXISTS
(SELECT *
FROM table2
WHERE table1.name = table2.name)
And NOT IN Cases.
But performance issues while a for large number of data.
I think your table table1 and table2 have index on their name column, so you can try this:
SELECT name
FROM table1 t1 LEFT JOIN table2 t2 ON t1.name = t2.name
WHERE t2.id IS NULL
May be id column existed, if not, use t2.name as a replacement for t2.id
For this query:
SELECT name
FROM table1
WHERE NOT EXISTS
(SELECT *
FROM table2
WHERE table1.name = table2.name)
You want an index on table2(name).
I want to make an incremental update of table1 with only new records from table2. I am looking for the fastest way. Assuming that the id is unique integer, which of these three approaches is the fastest? If you can propose any other approach, please share.
Solution 1.
INSERT INTO table1
SELECT * FROM table2
WHERE NOT EXISTS
(SELECT 1 FROM table1
WHERE table2.id = table1.id)
Solution 2.
INSERT INTO table1
SELECT table1.* FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
WHERE table2.id IS NULL
Solution 3.
INSERT INTO table1
SELECT * FROM table2
WHERE table2.id > (SELECT MAX([table1.id]) FROM table1)
The third solution is incomparably faster than two previous. But be careful. It may return different results than solution 1 and solution 2.
If you want to use 3rd solution be sure that new id in table2 always gets higher number than the older one. You may apply check of the records number in both tables. I also recommend indexing id in table1.
insert into table1
select table2.* from from table2 left outer join table1 on table2.id = table1.id
where table1.id is null
I have a SQL Insert statement that needs to insert records into another table only if the the record doesn't exist in table2 or the zip code has changes in table1. I have tried the following but it throws an error and it is the logic I am looking for:
INSERT INTO table2
SELECT id, zip
FROM table1 t1
JOIN table2 t2
ON t1.id = t2.id and t1.zip <> t2.zip
I also need it to insert the records if the id doesn't exist at all in table2. I have googled the crap out of this and can't seem to find the solution anywhere.
What about this?
INSERT INTO table2
SELECT t2.id, t2.zip
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.id = t2.id
WHERE (t1.id IS NULL OR t2.zip <> t1.zip)
Also, be sure to clarify which table's id and zip columns you are asking for.
You should always include column lists when doing inserts. Second, your query doesn't quite capture your logic. You need a left outer join to find the records that don't exist in the second table. Perhaps this might do what you want:
INSERT INTO table2(id, zip)
SELECT id, zip
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id
WHERE (t1.zip <> t2.zip) or (t2.zip is null)
You just need a WHERE NOT EXISTS clause
INSERT INTO table2
SELECT id, zip
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id AND table2.zip = table1.zip)
I have two tables Table1 and Table2. I compared thes tables using the query below. SELECT
Select Table1.ID
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
and got 1508 records that exist in Table1 but not in Table2.
Now I wanted to delete these records in Table2. This is the code I used below
DELETE Table1.*
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
This is the error I am get Could not delete from specified tables. I realize something is wrong with my sql, but where. I thought this DELETE was specifying a table not tables.
Try this:
DELETE FROM Table1
WHERE ID NOT IN
(
SELECT ID
FROM table2
)