copy column values from one column to other - mysql

I am trying to copy data into one column (which is null for now) in table A from another similar column in table B. One condition I have is that I only want to copy values from the column in table B that is NOT NULL.
So far I have:
UPDATE a
SET a.[null column] = b.[original column]
FROM A as a
INNER JOIN B as b
ON a.id = b.idx
Any idea on how to do this?

.... where b.[original column] is not null

UPDATE a
INNER JOIN b AS b ON b.idx = a.id
SET a.column_name = b.column_name
WHERE a.column_name IN NULL
You can use a join like this. If there were schema it would be better

UPDATE a
SET a.[column] = b.[column]
FROM A as a
INNER JOIN B as b
on a.id = b.idx and b.[column] is not null

Related

Updating a column in a MySQL table based on some comparisons in another table

I'm new to MySQL and need some help to achieve this result. My question goes as follows:
I have two tables A and B where table B is subset of table A.
Then, I add another column (say flag1 having default value 0) in table A.
I want to set value flag1=1 in table A where all the rows from table B are matching.
How can I achieve this in MySQL? Any help would be appreciated
UPDATE TableA AS a
RIGHT JOIN TableB AS b
ON a.col1 = b.col1
AND a.col2 = b.col2
AND a.col3 = b.col3
AND a.col4 = b.col4
AND a.col5 = b.col5
AND a.col6 = b.col6
SET a.flag1 = 1
The Right Join restricts the rows of TableA to the ones present in TableB and being identical on all six columns.
sqlfiddle

Selecting Rows from one table based on conditions in two separate tables

I've tried using joins and wherein statements for the following but I either get a timeout because it's taking too long to run or I get duplicate column name error.
I have 3 tables:
A, B, C
I'd like to create a table consisting of rows from A based on constraints in B and C. So The row in A has to fulfill a condition in B OR C:
(A.ID = B.ID and A.PURCHASE = B.PURCHASE) OR (A.ID = C.ID AND A.PURCHASE = C.PURCHASE).
I've been using mysql for around... a week and this is the closest I've gotten(it hangs):
CREATE TABLE D
SELECT T1.*
FROM TABLE A AS T1, TABLE B AS T2, TABLE B AS T3
JOIN T2, T3 ON ((T1.CUSTOMER_ID = T2.CUSTOMER_ID AND T1.DAY_ID =
T2.DAY_ID) OR (T1.CUSTOMER_ID = T3.CUSTOMER_ID AND T1.DAY_ID = T2.DAY_ID));
Thanks for any help!
Join together A, B, and C using LEFT JOIN, and then retain records from A where a match occurred in either B or C.
INSERT INTO D
SELECT DISTINCT a.* -- remove duplicate records
FROM tableA a
LEFT JOIN tableB b
ON a.CUSTOMER_ID = b.CUSTOMER_ID AND a.DAY_ID = b.DAY_ID
LEFT JOIN tableC c
ON a.CUSTOMER_ID = c.CUSTOMER_ID AND a.DAY_ID = c.DAY_ID
WHERE b.CUSTOMER_ID IS NOT NULL OR -- retain records where either
c.CUSTOMER_ID IS NOT NULL -- condition matches
If the D table is not already created, then create it using the same definition as for table A.

Make updates in one mysql table depending on the content of another table

I have two tables, A and B.
A has 'id' as primary key. B has (id, alpha) as primary key.
There is a column 'alpha' in A too. There is another column 'beta' in B.
I want to make the following query:
Update A, set A.alpha to ---> that B.alpha such that B.id = A.id and B.beta = (max(beta) for that particular id). Also, this should happen for only those A.id's whose alpha value is not equal to any of the alpha values in B for that same value of id. Not otherwise. Can anyone tell me how to do this?
This may not be the cleanest solution you get, but I believe it hits all your requirements:
update a, b
set a.alpha = b.alpha
where
a.id = b.id and
a.alpha not in (
select alpha
from b b1
where b1.id = a.id
) and
b.beta = (
select max(beta)
from b b2
where b2.id = a.id
)

mysql cross-updating a table

I can't figure out how to solve this problem.
I have a table (A) I need to update, with a structure like this:
CODE VALUE
1 a
2 null
3 null
etc...
Then I have another table (B) with the same structure but with every value set:
CODE VALUE
1 a
2 b
3 c
What I need to do is to copy data from table B to table A where A.CODE = B.CODE but only if A.VALUE is not set.
What's the best query to do so? Can't do it by hand since I'm dealing with 2000ish rows.
I wrote something like this but it doesn't seem to work:
update A set VALUE =
(select b.VALUEfrom B b, A a where b.CODE = a.CODE)
Thanks in advance!
UPDATE a SET a.Value = b.Value
FROM TableA a
INNER JOIN TableB b ON (a.CODE = b.CODE)
WHERE a.VALUE IS NULL
Check that a.VALUE is set to do the update, and only select from the B table in the inner clause (and use the value of A from the outer clause):
update A
set VALUE = (select B.VALUE from B where B.CODE = A.CODE)
where VALUE IS NULL;

Update table based on ID correspondence

I need to copy data from one table (A) to another (B) based on id correspondence, but the correspondence of ID is actually stored into a third table (C).
So the table with the correspondence looks like
C.A_ID C.B_ID
1 33
2 56
3 74
I tried something like
UPDATE DB.A
SET DB.A = DB.B
FROM DB.A p
INNER JOIN
DB.B p1
INNER JOIN
DB.C p2
how to insert the ID correspondence?
UPDATE A , (select c.id1, b.data from B join C on (B.id2 = C.id2 )) as Foo
SET A.data = Foo.data
WHERE
A.id1 = Foo.id1
you need to use select into statement for selecting data from one table and insert in another table.