Move records with unique field into 2nd table - mysql

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)

Related

copy tree structure in MySql and update ids

I have below structure of tables
table_a
table_b
table_c
table_c has external keys in table_b and table_b in table_a, all tables has relation one to many (one parent)
all tables have columnt environment_id
I have to copy structure for 2 rows from table_a and past to the same table with other environment_id
if I run script
insert into table_a (environment_id, text, is_default, type)
(select -1001, text, is_default, type
from table_a
where environment_id = -1005);
insert into table_b
(environment_id, a_id, ordinal, text, level)
(select -1001,
(select formulaid from table_a where environment_id = -1001 and text =
(select text from table_a where formulaid = a_id))
ordinal, text, level);
insert into table_c
(environment_id, b_id, languageid, translation)
(select -1001, (select id from table_b
where environment_id = -1001 and text = (select text from table_b
where environment_id = -1005 and text = (
select text from table_b where id = b_id))),
languageid, translation);
I will receive expected result for 1st table, but how to manage with rest?
I want to new rows from table_b point new rows from table_a, same with table_c and table_b.
You have to join same table with
select * from table_a first left join table_a second
on first.text = second.text
and get new id by comparing text field

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;

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

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

Inserting distinct entries into the database

I have two tables with exactly the same fields. Table A contains 7160 records and table B 7130 records.Now I want to insert distinct records from table A into table B such that B should not have any duplicate entry in it. How should I go about doing this?
This basically selects records that are in A that are not in B. It would work, but you might have to tweak the field you use to uniquely identify a record. In this example I used field 'ID' but you might have to change that to A.field1 = B.field1 AND A.field2 = B.field2 etc.
INSERT INTO TABLEB
(
SELECT A.*
FROM TABLEA A
LEFT JOIN TABLEB B ON A.ID = B.ID
WHERE B.ID IS NULL
)
You can use a "union" query to combine the results from multiple tables into a single result set. "union" will only return distinct rows from all tables.
See this page for more info:
http://www.tutorialspoint.com/mysql/mysql-union-keyword.htm
insert into tableB (id)
select t1.id from tableA t1
where t1.id not in (select t2.id from tableB t2)