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
Related
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)
Lets say i have a database with the following two tables:
Table_A Table_B
id val_A
val ...
...
Now i need to do the following SELECT on Table A:
SELECT id, val, ..., isInB FROM Table_A WHERE ....
Where ... is any other fields from Table_A that i need and isInB will be either 1 or 0 depending on wheter or not Table_A.val exists in Table_B.val_A
Can this be done?
You just have to LEFT JOIN both tables:
SELECT ta.id,
ta.val,
CASE WHEN tb.val_A IS NULL THEN 0 ELSE 1 END AS isInB
FROM table_A ta
LEFT JOIN table_B tb ON ta.val = tb.val_A
See this fiddle.
Try this
SELECT Table_A.id, Table_A.val, IF(table_B.val_A IS NULL, 0, 1) as isInB
FROM
Table_A
LEFT OUTER JOIN Table_B on Table_A.val = Table_B.val_A
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
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
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