JOIN mysql for updating the table - mysql

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;

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

Move records with unique field into 2nd table

I have 2 tables. table_a is current data, table_b has updated data. The 1st thing I need to do is move all new records, i.e., move the records from table_a to table_b that have a value in a primary index field (primaryField) not found in table_a.
I've tried variations of the following:
INSERT INTO table_b (`col1`,`col2`,`col3`,etc...)
VALUES (`col1`,`col2`,`col3`,etc...)
FROM table_a
WHERE table_a.primaryField NOT IN (SELECT table_b.primaryField)
This approach doesn't work. How do you select only the rows in a table that have values for a specific field not found in the matching field of a 2nd table?
You can LEFT JOIN table_a to table_b and then insert only those records in table_a which do not match anything in table_b.
INSERT INTO table_b (col1, col2, col3)
SELECT a.col1, a.col2, a.col3
FROM table_a a LEFT JOIN table_b b ON a.primaryField = b.primaryField
WHERE b.primaryField IS NULL
SELECT primaryField
From Table_a
WHERE primaryField NOT IN (SELECT primaryField FROM Table_b)
and you query can be
INSERT INTO table_b (`col1`,`col2`,`col3`,etc...)
VALUES (`col1`,`col2`,`col3`,etc...)
FROM table_a
WHERE table_a.primaryField NOT IN (SELECT primaryField FROM table_b.primaryField)

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

Compare two tables 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

MySQL query rows that are in table_b and not in table_a

I'm doing this query to get rows that are in table_b and not in table_a:
SELECT table_b.* FROM table_b
LEFT JOIN table_a ON table_b.id = table_a.id
WHERE table_a.item_id IS NULL
This works ok but I need another condition to apply on table_a. I need to compare only the rows that got column X equal with the ID of 3, not to compare the whole table. Any ideas ?
Thanks.
I'm not sure if you're looking for table_a column x or table_b column x, but the idea is to add that to the join ON clause:
LEFT JOIN table_a ON table_b.id = table_a.id and table_a.id = 3
Try this out...
SELECT table_b.*
FROM table_b
LEFT JOIN table_a
ON table_b.id = table_a.id
AND table_a.X = 3
WHERE table_a.item_id IS NULL
Should do the trick...
But this is a bit strange, as it will probably return more than you require. This query will also return any rows from table_b which do have a result in table_a, but where those rows don't have an 'X' value of 3.
If it doesn't work, please try to give an example for data that you need to find, and data that you don't want, and we can try to help further!