Compare two tables MySQL - mysql

I have two similar tables table_a and table_b
table_a is my current data and I need to update this info using table_b which is a temporary table. The only difference between the two is table_a has a password field that table_b will not have.
I am trying to do a couple of things.
1.Compare the data based on the "user_id" field.
2. If there is a user_id not found in table_b but there is one in table_a remove that row in table_a
3. If there is a user_id in table_b that is not found in table_a add that row of data to table_a
4. If the user_id is found in both then check two of the fields "email" and "job_code" and make sure they are both whatever table_b says.
Should I do this in separate MySQL statements in the order I have numbered above? below is my try at the above statements. Any help or troubleshoot is greatly appreciated. Thanks!
Statement 1:
SELECT * FROM table_a
WHERE table_a.user_id
NOT IN (
SELECT table_b.user_id
FROM table_b
WHERE table_a.user_id=table_b.user_id
) // But how do I delete those records that are selected?
Statement 2:
SELECT * FROM table_b
WHERE table_b.user_id
NOT IN (
SELECT table_a.user_id
FROM table_a
WHERE table_a.user_id=table_b.user_id
) //How do I Insert these records that I have in table_b but not in table_a
Statement 3:
SELECT email FROM table_b
WHERE table_b.user_id = table_a.user_id,
AND table_b.email != table_a.email //now I need to update table_a with the emails from table_b

Statement #1
DELETE A.*
FROM table_a A
LEFT JOIN table_b B
USING (user_id)
WHERE B.user_id IS NULL;
Statement #2
INSERT INTO table_b (column1,column2,...,columnN)
SELECT A.column1,A.column2,...,A.columnN
FROM table_b B LEFT JOIN table_a A
USING (user_id)
WHERE A.user_id IS NULL;
Statement #3
UPDATE table_a A INNER JOIN table_b B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
UPDATE 2012-06-19 11:54 EDT
A and B are just aliases for the table.
For example, the query of Statement #3 could have been written like the following:
UPDATE table_a as A INNER JOIN table_b as B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
or with no aliases whatsoever, like this:
UPDATE table_a INNER JOIN table_b USING (user_id)
SET table_a.email = table_b.email,table_a.job_code = table_b.job_code;

The solution of the first problem ::
1)
delete FROM table_a
left join table_b
on
table_a.user_id=table_b.user_id
and table_a.user_id is null

Related

How to to JOIN 2 tables with foreign key pointing to other table primary key to return all the row that have such primary key in SQL?

Please help I need to get rows in the 2 tables with the foreign key poiting to table_a as seen on picture. I have tried Joining this way,
SELECT * FROM table_a
INNER JOIN table_b
ON table_a.id = table_b.my_col
INNER JOIN table_c
ON table_a.id = table_c.my_col
but it return empty results. Please help me to fix such SQL JOIN statement
PLEASE LOOK AT THE PHOTO I MADE WHICH SHOW THESE TABLES AND MORE ELABORATION
could be that you have not valid match in table_b and table_c (your id for table_a don't match values in table_b and table_c)
try use left join
SELECT *
FROM table_a
LEFT JOIN table_b ON table_a.id = table_b.my_col
LEFT JOIN table_c ON table_a.id = table_c.my_col

JOIN mysql for updating the table

I have two tables in the mysql as follows:
Table_A
Table_B
I need to join Table_B to Table_A and add the values of BalAmountLC to Table_A where the GLAccount equals. I also need to update other GLAccount values to Table_A which is not exist in Table_A.
Anyone please advise me which is the best way to do it. I feel like I need to use RIGHT JOIN. But I am not sure about that.
I am stuck with the following query:
SELECT Table_A.*,Table_B.*,Table_A.BalAmountLC+Table_B.BalAmountLC as sum FROM Table_A RIGHT OUTER JOIN Table_B ON Table_A.GLAccount=Table_B.GLAccount
Thanks.
So basically you need to INSERT all GLAccount in Table_B to Table_A if it does not exit in Table_A. If exists, you want to add BalAmountLC to Table_A
UPDATE Table_A INNER JOIN Table_B ON (Table_A.GLAccount = Table_B.GLAccount)
SET Table_A.BalAmountLC = Table_A.BalAmountLC + Table_B.BalAmountLC;
INSERT INTO Table_A SELECT Table_B.* FROM Table_B LEFT JOIN Table_A
ON (Table_B.GLAccount = Table_A.GLAccount) WHERE
Table_A.GLAccount IS NULL;

Delete from table B where value does not exists in Table A

I am trying to accomplish the following in MySQL:
delete from table_b where table_b.token is not found in table_a.token
Explanation:
Both tables have a column called token.
I want to delete all records on table_b if token in table_b does not exist in the token
column in table_a.
Using a subquery:
delete from table_b where token not in (select token from table_a)
You can use a join
delete b.*
from table_b b
left join table_a a on(b.token = a.token)
where a.token is null

mysql update a table from another table performance

I have two mysql tables,
table_A = 2500000+ (rows)
table_B = 6000000+ (rows)
I need to update rows in table_A from data in table_B (using a multithreaded application)
What is the fastest way
Option A
update table_b b (select col_a,col_b from table_a where col_c=%s) b set a.col_a=b.col_a, a.col_b=b.col_b flag='C' where col_c=%s
Option B
data=select col_a,col_bfrom table_a where col_c=%s
update table_b set col_a=%s,col_b=%s,flag='C' where col_c=%s
Option C
left join table_b with table_a

how to get results based on column's result type from 2 other tables

i have a table called users contained
(Table A)
Users
user_id
username
user_type[1,2]
(Table B) if user_type=1
user_id
full_name
(Table C) if user_type=2
user_id
full_name
i want to get single record set by executing single query, is that possible in PHP mysql.
Try this:
SELECT table_a.*, COALESCE(table_b.full_name,table_c.full_name) AS full_name
FROM table_a
LEFT OUTER JOIN table_b ON table_b.user_id = table_a.user_id
LEFT OUTER JOIN table_c ON table_c.user_id = table_a.user_id WHERE 1;
It uses the LEFT OUTER JOIN, which means that it joins it to table_b on the given condition. However, for each row in table_a, whether it finds a matching row in table_b or not, it will return the table_a row. If it does not find a matching row, the table_b columns are just NULL. Same thing with table_c.
Then, we just select all the table_a columns. However, we now have two full_name columns, one from table_b and one from table_c. We use COALESCE to merge them.
COALESCE returns the first non-NULL value.
Since we know that there is either a matching row in table_b or a matching row in table_c, it is not a problem. However, it would be a problem if somehow you allows a matching row to be found in both table_b and table_c.
The risk can be mitigated by adding additional ON clause conditions to get:
SELECT table_a.*, COALESCE(table_b.full_name,table_c.full_name) AS full_name
FROM table_a
LEFT OUTER JOIN table_b
ON table_b.user_id = table_a.user_id AND table_a.user_type = 1
LEFT OUTER JOIN table_c
ON table_c.user_id = table_a.user_id AND table_a.user_type = 2
WHERE 1;
Still, you will need to make sure only 1 row is present for each user in table_b and table_c.
Instead of COALESCE you can optionally use CASE like:
SELECT table_a.*, CASE user_type WHEN 1
THEN table_b.full_name
ELSE table_c.full_name END AS full_name
...
or use an IF function like:
SELECT table_a.*, IF(user_type=1,table_b.full_name,table_c.full_name) AS full_name
...
You can UNION both tables and later on JOIN it with tableA
SELECT a.User_ID,
a.`username`,
b.full_name,
a.user_type
FROM tableA a
(
SELECT user_ID, full_name
FROM tableB
UNION
SELECT user_ID, full_name
FROM tableC
) b ON a.User_ID = b.User_ID
-- WHERE a.user_type = 1 -- add extra condition here