I have one a SQL query.
INSERT INTO `t1`(col1, col2)
SELECT * FROM `t2`
WHERE NOT EXISTS (
SELECT*FROM `t1`
WHERE (
t1.col1 = t2.col1
AND
t1.col2 = t2.col2 )
);
This query compares two existing tables "t1" and "t2".
Inserts data from table “t2” into table “t1” without any duplicate records between the two tables.
How can I truncate this SQL query? Can it be written in another way? Easier?
Yes, correlated suqbuery could be rewritten as LEFT JOIN:
INSERT INTO t1 -- I suggest to explicitly list all columns
SELECT t2.*
FROM t2
LEFT JOIN t1 -- USING (col1, col2) -- instead of ON clause
ON t1.col1 = t2.col1
AND t1.col2 = t2.col2
WHERE t1.col1 IS NULL;
Related: Blind insert
If (col1,col2) is a unique key in t1, you can just
insert ignore into t1 select * from t2;
Related
Can we use MERGE statement between 2 tables with different columns?
I need to update few columns in target table T1 from source table T2 based on one condition(where T2.Song_code=T1.Song_code).
But t1 has some columns which are not available in Source table. So did not exactly get how it could be used to see if the rows match.
Can someone please explain?
SQL
MERGE INTO t1 AS target
USING t2 AS source
ON target.Song_code=source.Song_code
WHEN MATCHED THEN
UPDATE SET target.Song_name = source.Song_name -- columns to update
WHEN NOT MATCHED BY TARGET THEN
INSERT VALUES (source.Song_code,source.Song_name); -- leave empty string (' ',' ') for Column with no value
You can try like this :
INSERT INTO T2 (col1, col2, col3, col4)
SELECT t1.col1, t1.col2, t1.col3, t1.col4
FROM t1
WHERE T2.Song_code=T1.Song_code
Use JOIN with UPDATE statement
UPDATE TABLE1 T1
JOIN TABLE2 T2
ON T2.Song_code=T1.Song_code
SET TABLE1.col1 = TABLE2.col1,
TABLE1.col2 = TABLE2.col2
I have a query like this:
UPDATE t1
SET t1.col1 = ( SELECT col1 FROM t2 WHERE <some_complex_conditions> ),
t1.col2 = ( SELECT col2 FROM t2 WHERE <some_complex_conditions> )
WHERE id = :id;
As you see, I have to execute the same query twice, every time for one column. Also as I've mentioned, that SELECT query has some complex conditions which need lots of processing. Now I want to know, how can I handle the UPDATE statement to get the update those two columns by single SELECT statement?
Something like this:
SELECT col1, col2 FROM t2 WHERE <some_complex_conditions>
In other word, how can I use this ^ into the UPDATE statement?
see multi table syntax in manual and stackoverflow
It's possible to update joined table. I think that it should be possible to join select statement, but I'm not sure, but I think example below shows equivalent query:
UPDATE t1
LEFT JOIN t2
-- joining condition, there can be the part <some complex condition>
ON t1.id = t2.ref_id AND t2.col3 = 'whatever you want'
SET t1.col1 = t2.col1, t1.col2 = t2.col2
--additional condition like WHERE t2.col1 IS NOT NULL
I am using mysql and I'm interested in rows where reciprocals appear in a different row in the table.
Imagine 2 columns, each with letters a through z.
Lets say row1 has a,b, row2 has a,c, and row 3 has c,a. I am interested in the pair a,c because it appears both as c,a and a,c in different rows in the table.
Do I have to use a nested select? Or perhaps an exists clause?
I believe this is what you're after, a self-join:
SELECT t1.*
FROM table1 t1
JOIN table1 t2
ON t1.col1 = t2.col2
AND t1.col2 = t2.col1
Here is a SQL fiddle demo: SQL Fiddle
also use SELECT REVERSE('abc') see http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_reverse with above #spencer7593 said
`SELECT t1.*
FROM table1 t1
JOIN table1 t2
ON t1.col1 = t2.col2
AND t1.col2 = t2.col1`
I have two tables. t1 and t2. I want to select the values that are from t1 and occurred in t2. What I wrote is the following:
select col1 from t1, t2
where t1.col1y=t2.col2;
Is this is the right way to search for the value ?
Yes, that works, but it's the old form of writing a join. Use the new form:
select t1.col1
from t1
inner join t2 on t1.col1 = t2.col2
The following will get me values from a reference table t2 I want to insert to or to update existing tuple with in table t1:
SELECT
id, col1
FROM
t2
LEFT OUTER JOIN
t1
ON
t2.id=t1.id
If a tuple with id already exist in t1, it should be updated with the value selected from t2. If a tuple with id does not exist in t1, (id, col1) should be inserted with other columns set to default values.
How to do this efficiently?
use this two querys:
This will join and filter, giving you the values that exists in both tables, so you just do the update
Update t1 set t1.col1 = t2.col1
from t1 inner join t2 on t1.id = t2.id
This will join and filter, giving you the values that are in t2, but not in t1, so you just do the insert.
insert into t1 select t2.id, t2.col1
from t2 left outer join t1 on t2.id = t1.id where t1.id IS NULL
UPDATE:
as I can see from here MySQL uses another sintax for this. So you query may work with this instead of the query above:
UPDATE t1 temp1
INNER JOIN t2 temp2
ON temp1.id= temp2.id
SET temp1.col1= temp2.col1
but the concepts are the same (just a different syntax)
You won't need the Where, because the INNER JOIN will only use fields that match/join.