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
Related
The title may be misleading but i don't know how to formulate it better.
Suppose i have these rows on my MySQL table:
table1:
id column1 column2
1 1 2
2 1 3
3 2 1
4 3 4
I have written a query to retrieve data that have similar vice-versa values on columns column1 and column2 (id: 1 & id: 3), but I'm having trouble querying over data that don't have similar vice-versa rows (id: 2 & id: 4) or that are sort of unique.
EDIT: The query i've used to get vice-versa rows
SELECT * FROM table1 t1
INNER JOIN table1 t2
ON (t1.column1 = t2.column2 AND t1.column2 = t2.column1);
You could use exists logic here:
SELECT id, column1, column2
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE LEAST(t2.column1, t2.column2) = LEAST(t1.column1, t1.column2) AND
GREATEST(t2.column1, t2.column2) = GREATEST(t1.column1, t1.column2));
A simple solution would be to use your query in the WHERE clause to filter out similar rows:
SELECT *
FROM table1 t1
WHERE (column1, column2) NOT IN
(SELECT t1.column1, t1.column2
FROM table1 t1
INNER JOIN table1 t2
ON t1.column1 = t2.column2 AND t1.column2 = t2.column1)
Demo here
Say I have an id in table1 which is a foreign key in table2 and there is a column in table2 called condition.
I need to select all ids from table 1 that aren't in table2 where condition = 1.
So for id in table 1 "select it" if it is not in table2 where condition = 1.
Edit: I used Ahsan Habib's answer and it worked great!
if you just want to select ID column from table1 this will work fine....Its just a simple set operation
select id from t1
minus
select id from t2 where condition = 1;
for all column you may try
select * from t1 whare id not in (select id from t2 where condition = 1);
This is almost a direct translation of what you are asking for:
select t1.*
from table1 t1
where t1.id not in (select t2.id from table2 t2 where t2.condition = 1);
Another way using NOT EXISTS
select t1.*
from table1 t1
where NOT EXISTS(select 1 from table2 t2 where t1.id = t2.id and t2.condition = 1);
I am stuck in querying sql database for a scenerio.
Table 1:
comp col1 col2 col3
nam1 0 0 a
nam2 2 2 b
nam3 5 4 c
table 2:
comp col1
nam1 2
nam2 2
My result should be col1 value 0 in table1 and the same comp value should present in table2 with value in col1..
My result be:
comp col3(table1) col1(table2)
nam1 a 2
thanks in advance.
Try joining both the table like :
SELECT t1.col3, COUNT(t2.col1) AS col1
FROM table1 t1 INNER JOIN table2 t2
ON t1.comp = t2.comp
WHERE t1.col1 = 0
GROUP BY t2.col1
You simply need to join your tables:
SELECT comp, table1.col3, table2.col1
FROM table1 JOIN table2 USING (comp)
WHERE table1.col1 = 0
[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;
My table scheme is as follows: (Bold column name is primary key)
Table 1: id1 - id2
Table 2: id2 - name2
Table 3: id3 - name3
Table 4: id1 - Id3
What I want to do is have sql code that :
Select data in id1 and id3 columns for which name2=input=name3
Insert into table 4
Only insert into 4 if id1, id3 combination doesn't exist in table 4
Currently I can do step 1 and 2, but (assuming it can be done) I cannot get the syntax for "NOT EXIST" correct for step 3.
This is my code currently:
INSERT INTO table4( id1, id3)
SELECT id1, id3
FROM table2
INNER JOIN table1 ON table1.id2 = table2.id2
INNER JOIN table3 ON table2.name2 = table3.name3
WHERE name2 LIKE 'input'
Here the query you need
insert into table4(id1, id3)
select t1.id1, t3.id3
from table2 as t2
inner join table1 as t1 on t1.id2 = t2.id2
inner join table3 as t2 on t2.name2 = t3.name3
where
t2.name2 like 'input' and
not exists (
select *
from table4 as t4
where t4.id1 = t1.id1 and t4.id3 = t3.id3
)
as an advice - I suggest you always use aliases (and refer to column as alias.column_name) in your queries, it'll help you to avoid bugs and your queries will be more readable.
I think you are looking for this
INSERT INTO table4( id1, id3)
SELECT id1, id3
FROM table2
INNER JOIN table1 ON table1.id2 = table2.id2
Left JOIN table3 ON table2.name2 = table3.name3
WHERE name2 LIKE 'input' and table3.name3 is null
or something similar. Left (outer join) gets all the records in table2 whether they exist or not. If they don't table3.name3 will be null, so those are the chaps you want.
your current query is ok for the insertion,
but if you want to deny inserts if that combination already exists,
simply add a primary key to table4, that contains those 2 columns.
In the query do:
INSERT INTO table4( id1, id3)
SELECT id1, id3
FROM table2
INNER JOIN table1 ON table1.id2 = table2.id2
INNER JOIN table3 ON table2.name2 = table3.name3
WHERE name2 LIKE 'input'
ON DUPLICATE KEY UPDATE id1=id1;
that is just for making the query still run,
if there is a duplicate it will do nothing.