MySql - updating 2 columns simultaneously from another table - mysql

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

Related

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)

Retrieve data using joins with multiple conditions

I am trying to write a query which retrieves data from a table which doesn't have data on another table.
I have table A with values
ID StudentID CLASSID
1 1 2
2 2 3
3 3 4
4 4 5
TABLE B with values
ID StudentID CLASSID
1 1 2
2 2 3
I am trying to return values from table A with ID 3,4 which is not available in TABLE B.
Query I have tried is
SELECT *
FROM A AS a
WHERE NOT EXISTS
(
SELECT *
FROM B AS b
WHERE a.student_id = b.student_id
AND a.CLASSID = b.CLASSID
);
NOTE: As my problem was slow query. I have fixed this problem by creating index which made this query run fast.
Thanks for your effort.
Using LEFT OUTER JOIN
SELECT TableA.* FROM TableA
LEFT OUTER JOIN TableB
ON TableA.ID = TableB.ID
WHERE TableB.ID IS null
Using NOT IN
SELECT * FROM TableA
WHERE TableA.ID NOT IN ( SELECT ID FROM TableB)
Using NO EXISTS
SELECT *
FROM tableA a
WHERE NOT EXISTS
(
SELECT 1
FROM tableB b
WHERE a.studentsID = b.studentsID
AND a.CLASSID = b.CLASSID
);

Insert on each inner joined foreach row matched

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);

SQL Table Counting and Joining

I have Table A, Column 1.
This table has values such as:
1
2
3
3
4
4
4
5
I have Table B, Column 2.
Which lists certain values, like:
1
3
4
I need a query to Count each unique value in Table A, but ONLY if that value is present in Table B.
So with the above, the end result would be:
1 has a quantity of 1,
3 has a quantity of 2,
and 4 has a quantity of 3.
My only problem is that I do not have this ability. Any help out there?
Based on your question, something like the following should solve your problem.
select b.column1,
count(a.column2)
from tableb as b
inner join tablea as a on b.column1 = a.column2
group by b.column1
Since you wanted only records which are in both tables, I am using an inner join. Then I am just grouping by the ID found in tableb, and getting the count of rows in tablea.
Let me know if you have any problems.
For more information regarding inner join, see : http://www.w3schools.com/sql/sql_join_inner.asp, and for group by, see : http://www.w3schools.com/sql/sql_groupby.asp
I would use an INNER JOIN query with GROUP BY aggregate function
SELECT a.column1,
count(a.column1) as total
FROM tablea a
INNER JOIN tableb b
ON a.column1 = b.column2
GROUP BY a.column1
SELECT column1,COUNT(column1)
FROM table1
WHERE column1 IN
(SELECT DISTINCT column2 FROM table2)
GROUP BY column1
Try this
MsSql
Select Distinct Column1,Count(Column1) Over (Partition by Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
Fiddle Demo
MySQl
Select Column1,Count(Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
group by column1
Fiddle Demo

how to get the table of missing rows in mysql

i have two mysql tables
tableA
colA1 colA2
1 whatever
2 whatever
3 whatever
4 whatever
5 whatever
6 whatever
second table is basically derived from tableA but has some rows deleted
tableB
colB1 colB2
1 whatever
2 whatever
4 whatever
6 whatever
how can i write an query to obtain the table of missing rows from the above two tables
i.e
colC1 colC2
3 whatever
5 whatever
SELECT t1.*
FROM TableA t1 LEFT JOIN
TableB t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
What about something like this :
select *
from tableA
where not exists (
select 1
from tableB
where tableB.colB1 = tableA.coldA1
)
i.e. you select the data from tableA for which there is no equivalent data in tableB.
select * from tableA where colA1 not in ( select colA1 from tableB ) ;