Replace a column of data with another column from another table - mysql

I would like to replace a column of data in a table.
TableA
Uid - int
AnotherUid - int
TableB
Uid - int
TableA.uid = Table.B uid
And I am trying to replace the TableB.Uid with TableA.AnotherUid
Select * from TableB a, TableA b where a.uid=b.uid
update TableB set a.uid=b.AnotherUid
I got a SQL syntax error from MySQL at TableB set a.uid=b.AnotherUid.
Please kindly help.

UPDATE TableB T
SET T.uid =
(SELECT AnotherUid
FROM TableA A
WHERE A.uid = T.uid)

UPDATE TableB SET TableB.Uid = (SELECT AnotherUid FROM TableA WHERE TableA.Uid = TableB.Uid)

Try this query:
Update TableB, TableA
Set TableB.uid = TableA.AnotherUid
Where TableB.uid = TableA.uid;
For MySQL manual on join in the Update query please refer: http://dev.mysql.com/doc/refman/5.0/en/update.html and see this example in their doc:
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;

Related

UPDATE A COLUMN WITH DIFFERENT VALUES IN ROWS

I want to update ColumnX of TableA with the values of ColumnY in TableB.
This two tables have in common the atrribute id.
Is it possible, when I try an UPDATE code I get
Subquery returns multiple rows
The subquery looks something like this:
UPDATE TableA
SET ColumnX = (SELECT ColumnY FROM TableB WHERE tableA.id=tableA.id);
Try:
UPDATE TableA
SET ColumnX = (SELECT ColumnY FROM TableB WHERE tableA.id=tableB.id);
And make sure that SELECT ColumnY FROM TableB WHERE tableA.id in (SELECT id FROM TableB ) returns 1 value
your where clause is currently using tableA.id=tableA.id, which will be true for every row. Try:
UPDATE TableA
SET ColumnX = (SELECT ColumnY FROM TableB WHERE tableB.id=tableA.id);
You can use joins like when you are selecting rows, for example:
UPDATE TableA, TableB set TableA.ColumnX=TableB.ColumnY WHERE TableA.id=TableB.id
Just change the name in your where clause condition after = as you have same table name resulting into multiple results to TableB.id or try below
UPDATE TableA A
Join TableB B
On
A.id= B.id
SET A.ColumnX = B.ColumnY

How to use update together with select...for update in mysql

I have two MySql tables tableA and tableB, primary key "id" in tableA is used as a foreign key "parent_id" in tableB. I would like to update single row in tableB using select...for update so that other users can not access it while transaction is not over. My question is - how to correctly update selected row in one query? Here is my sample code:
START TRANSACTION;
SELECT b.reserved, b.owner FROM tableB b, tableA a
WHERE b.parent_id = a.id AND a.guid ='5344a990-fedf-4deb-a114-0d5d6a3ba180' FOR UPDATE;
UPDATE tableB SET...;
COMMIT;
Thank you!
Yes, this is possible.
Please take a look at:
MySQL - UPDATE query based on SELECT Query
MySQL UPDATE
Here is an example of what your query may look like:
START TRANSACTION;
# Lock table using `FOR UPDATE`
SELECT
b.reserved,
b.owner
FROM
tableB b,
tableA a
WHERE
b.parent_id = a.id
AND a.guid ='5344a990-fedf-4deb-a114-0d5d6a3ba180'
FOR UPDATE;
# Update query
UPDATE
tableA
SET
tableA.column1=(
SELECT
b.reserved
FROM
tableB b,
LEFT JOIN tableA a ON a.id=b.id
WHERE
b.parent_id = a.id
AND a.guid ='5344a990-fedf-4deb-a114-0d5d6a3ba180'
)
WHERE ...
LIMIT 1;
COMMIT;
Hope this helps,

How to update a field of a table that is inner join of another table in MySQL?

I have two tables: tablea and tableb. Both of them have three columns called columna, columnb ,columnc.
Now I want to do this:
If tablea.columnb equals tableb.columnb,then set tablea.columnc = tableb.columnc. I have written the sql and it works well, but I think that there must be a better way to do this? Can anyone help me to optimize my sql statement, or is there any other way?
UPDATE tablea ta
SET
ta.columnc = (
SELECT columnc FROM tableb
WHERE ta.columnb = tableb.columnb
)
WHERE ta.columnb IN (
SELECT columnb FROM tableb
WHERE ta.columnb = tableb.columnb
)
You can try this simple query
Update tablea ta,table tb set ta.columnc=tb.columnc where ta.columnb =tb.columnb;

Joining on deletion

I'm working with mysql:
btableA has tableB_id column
tableB has some_interestring_column_on_TableB
I want (pseudo sql below):
delete from tableA
where the associated row in tableB (via tableB_id) has
some_interestring_column_on_TableB = 'interestingValue'
Please help me to translate the pseudo sql into real sql.
Try this:
DELETE TableA
WHERE tableB_id IN (
SELECT id FROM TableB
WHERE interestring_column='pizza');
MySQL supports JOINs in the DELETE statement, as well as deleting from multiple tables in a single statement. The following will only delete from TABLEA:
DELETE ta
FROM TABLEA ta
JOIN TABLEB tb ON b.id = a.tableb_id
AND b.col = 'some value'
If you wanted to delete from both tables, use:
DELETE ta, tb
FROM TABLEA ta
JOIN TABLEB tb ON b.id = a.tableb_id
AND b.col = 'some value'
That said, this support is very uncommon in other databases -- you'd have to use IN or EXISTS in most cases.
I don't know mysql syntax but I'm thinking something like (from mssql):
delete from tableA
where tableA.tableB_id in
(select tableB.id from tableB
where tableB.some_interesting_column_on_TableB = 'interestingValue')

How to update a table using a select group by in a second one as the data source in MySQL?

I can't do this in MySQL
UPDATE tableA, tableB
SET tableA.column1 = SUM(tableB.column2)
WHERE tableA.column3 = tableB.column4
GROUP BY tableB.column4
;
Neither can I
UPDATE tableA,
(
SELECT SUM(tableB.column2) sumB, tableB.column4
FROM tableB
GROUP BY tableB.column4
) t1
SET tableA.column1 = sumB
WHERE tableA.column3 = column4
;
Besides it being illegal code, I think you can understand what I tried to do with the queries above. Both of them had the same intent.
How can I do that in MySQL?
This would be one way, if you don't mind using a subquery:
UPDATE tableA
SET column1 = (
SELECT sum(column2)
FROM tableB
WHERE tableA.coumn3 = tableB.column4);