Insert on each inner joined foreach row matched - mysql

I have two tables, lets called them A and B. Here is what they look like
Table A:
ID BCODE
1 A1
2 B1
3 C1
4 D1
5 F1
Table B:
X Y IDX IDY
A1 D1
D1 F1
C1 B1
Table B has columns 'X' and 'Y' that have values that are found in Table A's column BCODE.
I want to insert the ID from Table A for each joined X,Y column with Table A's BCODE. Each matching BCODE for X goes to IDX. Each matching BCODE for Y goes to IDY. This is what it should look like
Table B:
X Y IDX IDY
A1 D1 1 4
D1 F1 4 5
C1 B1 3 2
I have a table A and B with about 50 million rows in both Table A and B
Does anyone know how I can do this ? I tried using INNER JOINS but it didnt populate all the columns. Thanks

you need a simple join with the Update command:
in MS SQL Server:
Update tableB
set
tableB.IDX=a.ID,
tableB.IDY=aa.ID
from
tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE
in MySQL:
Update tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE
set
tableB.IDX=a.ID,
tableB.IDY=aa.ID
(Edit: as question is about MySQL I just create The DEMO for it)
in Oracle PL/SQL:
Update (select tableB.IDX idx, tableB.IDY idy, a.ID ida, aa.ID idaa
from tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE)
set
idx=ida,
idy=idaa

UPDATE b
SET idx =
(SELECT id
FROM a
WHERE bcode = b.x),
idy =
(SELECT id
FROM a
WHERE bcode = b.y);

Related

MySQL find ids when match other table or not exists in it

I have two tables and I want a query to show records from one table that match the second table or doesn't exists at all. In other words if the record exists in table B and does not match the condition don't show it.
TABLE A TABLE B
ID VAL ID AID BVAL
--------- ----------------
1 v1 1 2 B1
2 v2 2 3 B2
3 v3
I've tried with this query:
SELECT ta.id, ta.val, tb.bval
FROM table_a ta
LEFT JOIN table_b tb ON ta.id = tb.AID AND tb.BVAL = 'B1'
the goal is to get ONLY this rows:
ID VAL BVAL
------------------
1 v1 NULL
2 v2 B1
But obviously with this query I get all Table A.
Thanks for your time!
I have found a solution, I post it in case someone else is having the same issue.
SELECT ta.id, ta.val, tb.bval
FROM table_a ta
LEFT JOIN table_b tb ON ta.id = tb.AID
GROUP BY ta.id, ta.val, tb.bval
HAVING tb.BVAL = 'B1' OR tb.BVAL IS NULL

SQL to fetch data where Unique key matches but the data is different in some other columns between different tables

I have two tables of same structure as below. I am trying to write a query to compare both the tables using the Unique key which is the first column and trying to return values when there is a mismatch in the second column.
If the key is not present then no need to consider that data. only if the key is present in both the table then we have compare it.
Table A
ColumnA ColumnB
A 1
B 2
C 2
D 8
Table B
ColumnC ColumnD
A 1
B 3
C 5
F 4
For example the output of the above table when comparing Table A with B should be
B 2
C 2
and when comparing Table B with A it should be
B 3
C 5
Ideally the difference in the base table should come.
I have tried Joins and Unions but I am not able to fetch the data as mentioned above.
Since you want only those rows which has matching FK values in both the tables, we simply need to use INNER JOIN.
Now, we can simply consider the unmatching rows by using WHERE .. <> ..
When comparing Table A against Table B, we can get Table A rows only:
SELECT
tA.*
FROM tableA AS tA
JOIN tableB AS tB
ON tB.ColumnC = tA.ColumnA
WHERE tB.ColumnD <> tA.ColumnB
When comparing Table B against Table A, simply fetch the rows from Table B only:
SELECT
tB.*
FROM tableA AS tA
JOIN tableB AS tB
ON tB.ColumnC = tA.ColumnA
WHERE tB.ColumnD <> tA.ColumnB
I would do :
SELECT t.*
FROM tablea t
WHERE EXISTS (SELECT 1 FROM tableb t1 WHERE t1.cola = t.cola AND t1.colb <> t.cold);
Same would be for second version just need to swipe the table names.
use EXISTS and union all
SELECT t.*
FROM tablea t
WHERE EXISTS (SELECT 1 FROM tableb t1 WHERE t1.cola = t.cola AND t1.colb <> t.colb)
union all
SELECT t.*
FROM tableb t
WHERE EXISTS (SELECT 1 FROM tablea t1 WHERE t1.cola = t.cola AND t1.colb <> t.colb)

Update query with self table based on column wise condition

I need to update the ManagerId as I have following table structure
userid EmpAccId ManagerAccId ManagerId
1 X Y
2 Y Z
3 Z X
I need to update the above table based on EmpAccId and ManagerAccId as for userId 1 The managerId should be 2 as EmpAccId for userid 1 is X and ManagerAccId is Y and Y's userid is 2 so for userid 1 MangerId should be 2.
so the resultant table should be
userId EmpAccId ManagerAccId ManagerId
1 X Y 2
2 Y Z 3
3 Z X 1
I need a single dynamic query in sql.
you can use a left join to achieve this :
UPDATE mytable as TB1
LEFT JOIN mytable as TB2
ON TB1.ManagerAccId = TB2.EmpAccId
SET TB1.ManagerId= TB2.userId
try this
UPDATE tbl_a t1
INNER JOIN tbl_b t2
ON t1.EMPAccId = t2.EMPAccId
AND t1.ManagerAccId = t2.ManagerAccId
SET t1.ManagerID = t2.ManagerID;
This will also help you
update table t1 left join table t2 on t1.EmpAccId=t2.ManagerAccId set t2.ManagerId=t1.userId;

MySql - updating 2 columns simultaneously from another table

I have 2 tables. One table is a list of all the possible unique values for column1 with 2 other columns of data.
The second table has 3 columns with the same values from column1 but repeated multiple times.
I have a query that works to copy columns 2 and 3 from TableA to TableB, but it takes forever to run. This is what I have:
TableA
column1 column2 column3
1 1 1
2 2 2
TableB
column1 column2 column3
1 a b
2 c d
3 e f
update TableA
set column2 = (
select column2
from TableB
where TableA.column1 = TableB.column1
);
update TableA
set column3 = (
select column3
from TableB
where TableA.column1 = TableB.column1
);
I tried to do it by using a JOIN, but that actually takes even longer.....
Any ideas?
You can try with an INNER JOIN query this way
UPDATE tablea a
INNER JOIN tableb b
ON a.column1 = b.column1
SET b.column2 = a.column2,
b.column3 = a.column3
If there are so many records it will take some times though

select from multiple tables

I have table AA with Columns
A B C with values 1,a,a
Table BB with A,B,C with values 2,b,b
if i select B=a and C=a i want to get o/p as Column A with Value 1
or
if i select B=b and C=b i want to get o/p Column A with Value 2
I want single query for this
(SELECT a FROM AA WHERE B = 'something' AND C = 'something')
UNION ALL
(SELECT a FROM BB WHERE B = 'something' AND C = 'something')