[MySQL 5.5]
I have two tables - Table_1 and Table_2.
They have identical columns - Col1, Col2, Col3, Col4.
Table_1 can have duplicates on columns Col1 and Col2.
Example-1:
Col1,Col2,Col3,Col4
1) a ,b ,c ,1
2) a ,b ,d ,2
Now Table_2 has the following rows:
Example-2:
Col1,Col2,Col3,Col4
1) a ,b ,e ,1
2) a ,c ,f ,2
I want to write all rows from Table_2 into Table_1 that do not have duplicates on Col1 and Col2. In the above instance, the insert should ignore row 1 in Example-2 above and add row 2 since there are no duplicates for combination (a,c) in Table_1.
Adding Unique keys on Col1 and Col2 will not work as it will delete row no 2 in Example 1.
Both Table_1 and Table_2 have 2 million rows each. Nested select statements(which I tried) have spelled disaster in terms of execution time.
Is there another way out of this?
This should do:
INSERT INTO Table_1
SELECT *
FROM Table_2 A
WHERE NOT EXISTS(SELECT 1 FROM Table_1
WHERE Col1 = A.Col1
AND Col2 = A.Col2)
insert into table1
select *
from table2
where concat(Col1,Col2) not in
(
select concat(col1,col2)
from table1
) as T
See below. Its using join so will have better performance.
INSERT INTO Table_1
SELECT T2.Col1
,T2.Col2
,T2.Col3
,T2.Col4
FROM Table_2 T2
LEFT JOIN Table_1 T1
ON T2.Col1 = T1.Col1
AND T2.Col2 = T1.Col2
WHERE T1.Col1 IS NULL
AND T1.Col2 IS NULL
Find the rows that don't exist via LEFT JOIN and NULL checking in the WHERE clause
INSERT INTO Table_1 (Col1, Col2, Col3, Col4)
SELECT Table_2.Col1, Table_2.Col2, Table_2.Col3, Table_2.Col4
FROM Table_2
LEFT JOIN Table_1
ON Table_2.Col1 = Table_1.Col1
AND Table_2.Col2 = Table_1.Col2
WHERE Table_2.Col1 IS NULL;
Related
I've two tables like below (database MYSQL):
Table1
id
col1
Table2
id
col1 -> foreign key(Table1 - id)
col2
Now I want to insert value into Table2(col2) for all rows with the following condition:
Get value from Table1(col1) where Table2(col1) = Table1(id)
Example:
Before Insert:
Table1
id col1
1 value1
2 value2
Table2
id col1(fk) col2
3 1 NULL
4 2 NULL
After Insert:
Table2
id col1(fk) col2
3 1 value1
4 2 value2
I tried insert into with select join and where but apparently couldn't get it to work
insert into Table2(col2)
select t1.col1 from Table1 t1 join Table2 t2 on t1.id = t2.col1
Any pointers ?
Update
Got it working. Thanks for the pointers #rahul #frank I actually need to do update
update Table2 t2
set col2 = (SELECT t1.col1 FROM Table1 t1 where t1.id = t2.col1);
Update with JOIN
-- MySQL
UPDATE Table2
INNER JOIN Table1
ON Table2.col1 = Table1.id
SET Table2.col2 = Table1.col1;
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a28fec2da45aa634f2509ec9299c2bed
I have 2 tables: tbl1 and tbl2. I want to return a single row from tbl1 with columns: col1, col2, col3, can_be_deleted, have_important_items. The idea is that can_be_deleted and have_important_items columns are boolean values resulted (both) by searching in the same table tbl2.
SELECT
col1,
col2,
col3,
NOT EXISTS(SELECT 1 FROM tbl2 WHERE mycategory=10 AND status>0 LIMIT 1) AS can_be_deleted,
EXISTS(SELECT 1 FROM tbl2 WHERE mycategory=10 AND type_item>0 AND status>0 LIMIT 1) AS have_important_items
FROM tbl1 WHERE ... LIMIT 1
To avoid later clarifications, tbl2 columns are:
mycategory - a value to group items inside table
status - enabled/disabled item
type_item - 0-not important, >=1 important one (scale of importance)
Question: Can I write a faster query?
using JOIN is faster than EXIST,check if this query solve your probleme and if not then just change the conditions to get your result.
"t1.t2_id" is the foreign key of tbl2, change it to the correct name
SELECT
col1,
col2,
col3,
LEFT JOIN tbl2 AS t2 ON t2.id = t1.t2_id
WHERE t2.mycategory = 10
AND t2.status>0
AND t2.type_item>0
FROM tbl1 AS t1 WHERE ... LIMIT 1
Is it possible to do an insert query like this?
INSERT INTO `table_1`
VALUES ('val1','val2','val3')
WHERE (
SELECT COUNT(*)
FROM `table_2`
WHERE col1='somevalue'
)=0;
You can do this using insert . . . select:
INSERT INTO `table_1`(col1, col2, col3)
SELECT col1, col2, col3
FROM (SELECT 'val1' as col1,'val2' as col2, 'val3' as col3) t
WHERE NOT EXISTS (SELECT 1
FROM table_2 t2
WHERE t2.col1 = 'somevalue'
);
Notes:
This seems really strange. Limiting inserts into table_1 when no row exists in table_2 with other values.
NOT EXISTS is more efficient than using COUNT(*).
You should always list the columns when using INSERT.
I have two tables, in which table 1 contains 4 columns while table 2 contains 8 columns. I have two columns in table1 that I want to compare them with two columns in table2.
Table 1 have column1 and column2 (that needs to be compared)
Table 2 have column6 and column7 (that needs to be compared)
I need to compare the combination of the two columns. I tried to do the below query however it doesn't work
Select * from table1
where column1, column2 NOT IN (Select column6, column7 from table2)
How can I compare the two columns in the the two tables?
Except shows the difference between two tables (the Oracle guys use minus instead of except and the syntax and use is the same). It is used to compare the differences between two tables. For example, let's see the differences between the two tables
SELECT * FROM
table1
EXCEPT
SELECT * FROM
table2
Try a minus statement. This will give you any results from the first select statement of table1 that aren't in the second select statement on table2.
select column1, column2 from table1
minus
select column6, column7 from table2
NOT EXISTS is a "null safe" version of NOT IN.
If you mean the combination column1 AND column2 not in same row in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6
and table1.column2 = table2.column7)
Or if you mean just column1 and column2 values can't even be in different rows in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6)
and NOT EXISTS (select 1 from table2
where table1.column2 = table2.column7)
The query with the least comparisions I can think of is
Select t1.*
from table1 t1
left join table2 t2 on t1.column1 in (t2.column6, t2.column7)
or t1.column2 in (t2.column6, t2.column7)
where t2.column6 is null
SELECT * FROM table1 t1
RIGHT JOIN table2 t2
WHERE
t1.c1 = t2.c6 AND
t1.c2 = t2.c7
Please try this query:
Select
case when (table1.column1 = table2.column6)
then 1 else 0
end column1_6 check,
case when (table1.column2 = table2.column7)
then 1 else 0
end
from
table1
inner join
table2 on table1.ID = Table2.ID
select * from table1 where column1 not in(select column 6 from table2) or column2 not in(select column7 from table2)
This will give you rows from table1 where there are differences between col1 and col6 or col2 and col7
Hope this helps
In MySQL, How do I copy a FIELD with all RECORDS from TABLE1 to TABLE2 which corresponds to a primary key ie: EMPLOYEE no.?
If you mean you want to update one table's column using another table's column, then here are some options:
A join:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo
SET t1.SomeColumn = t2.SomeColumn
Alternatively it could be a left join:
UPDATE table1 AS t1
LEFT JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo
SET t1.SomeColumn = t2.SomeColumn
which would essentially empty (set to NULL) the values where no match occurred.
A subquery:
UPDATE table1
SET SomeColumn = (
SELECT SomeColumn
FROM table2
WHERE EmployeeNo = table1.EmployeeNo
)
This is equivalent to the left join solution in #1.
Note that in all cases it is assumed that a row in table1 can match no more than one row in table2.
Try this
INSERT INTO `table2` (`field_name2`) SELECT `field_name` FROM `table1`
The query for copy data from one table to another is:
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
If you want to copy only selected values, then use where clause in query
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
WHERE `field1` = condition
update
table1 t1
join table2 t2 on t2.field = t1.field
set
t1.field1 = t2.matchingfield
where
t1.whatever = t2.whatever
You can use this to copy all the records from table1 into table2 with a condition.
Insert into table2 select * from table1 where field1=condition
Suppose if the table structure is as follows.
TableA - Col1, Col2 ,Col3
TableB - Col1, Col2 ,Col3
There is no need to select all column of the table to transfer data from 1 table to another table in same databse.
You can copy (insert) the rows from TableA to TableB.
Code as follows -
Insert into TableB (Col1, Col2 ,Col3)
Select Col1, Col2 ,Col3 from TableA
You can also do this -
Insert into TableB (Col1, Col2, Col3)
Select * from TableA
Both codes work , you need to see your requirement.
Generic code -
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
You can add 'Where' condition if you need.
Thank you!!!
INSERT INTO table_1(column-1, column-2) SELECT column-1, column-2 FROM table_2;
Insert into Delivery (DeliveredDate, appid, DownloadSize, UploadSize) select Delivered, Appid, DownloadSize,UploadSize from Delivery_Summary;