MYSQL UPDATE SELECT from Two Tables - mysql

Using the selected answer here, I attempted to craft a MySQL query that select columns and set values but am getting a 'Every derived table must have its own alias'. I only have two tables: matrix_swfl_res & RLN which I've defined as e & d respectively. What am I missing?
UPDATE (SELECT e.MLSNumber, d.MLSNumber
FROM matrix_swfl_res e, RLN d
WHERE e.MLSNumber = d.MLSNumber)
SET e.RSLN = d.RSLN

If you consider the answer linked, you have to do a Join when you want to update a table
Here the mysql error is because
(SELECT e.MLSNumber, d.MLSNumber
FROM matrix_swfl_res e, RLN d
WHERE e.MLSNumber = d.MLSNumber)
is considerated as a derivated table, as you write it, it's like you want to update this derivated table.
If I understand what you want :
You want to update the table A with some select you have done previously, here is what you need to do :
UPDATE A
INNER JOIN (SELECT e.MLSNumber, d.RSLN
FROM matrix_swfl_res e
INNER JOIN RLN d ON e.MLSNumber = d.MLSNumber ) as Q
ON A.MLSNumber= Q.MLSNumber)
SET A.RSLN = Q.RSLN
I don't have all the tables details, but I hope with this example it will be clearer

The second part of your linked answer is what you need - something like
UPDATE matrix_swfl_res e
INNER JOIN RLN d ON e.MLSNumber = d.MLSNumber
SET e.RSLN = d.RSLN

Related

How to format my UPDATE statement correctly? Getting not unique table/alias error

I am getting a "... not unique table/alias 'plots' ..." error when trying to run the following UPDATE statement:
UPDATE homestead.plots
INNER JOIN homestead.graphs
ON homestead.drivers.id = homestead.graphs.driver_id
INNER JOIN homestead.plots
ON homestead.plots.graph_id = homestead.graphs.id
SET homestead.plots.yAxis = homestead.plots.yAxis + 3.4
WHERE homestead.graphs.name = "DI";
Even though the below SELECT statement works fine, and returns the results I want:
SELECT homestead.graphs.driver_id, homestead.drivers.MarketingNo, homestead.graphs.name, homestead.plots.xAxis, homestead.plots.yAxis
FROM homestead.drivers
INNER JOIN homestead.graphs
ON homestead.drivers.id = homestead.graphs.driver_id
INNER JOIN homestead.plots
ON homestead.plots.graph_id = homestead.graphs.id
WHERE homestead.graphs.name = "DI";
Any ideas how to fix my UPDATE statement to work? I've done a lot of research online but cannot understand why this doesn't work.
There are several flaws with your UPDATE statement, for example:
table plots is referenced twice (in the UPDATE and in a JOIN) and not aliased (this is causing the error that you are getting)
you are referring to column id in table homestead.graphs, but this table is not part of any join
Based on your SELECT query, I would try and phrase your UPDATE as follows:
UPDATE homestead.plots p
INNER JOIN homestead.graphs g ON p.graph_id = g.id AND g.name = "DI"
INNER JOIN homestead.drivers d ON d.id = g.driver_id
SET p.yAxis = p.yAxis + 3.4

Column ' ' in field list is ambigious

i recieve an error
column expeertID in field list is ambiguous
im not sure how to fix this
INSERT INTO cv(expertID)
SELECT expertID
FROM experts
INNER JOIN cv ON experts.expertID = cv.expertID;
Try below - you need to add tablename, as expertID column exists in both tables
INSERT INTO cv(expertID)
SELECT experts.expertID
FROM experts
INNER JOIN cv ON experts.expertID = cv.expertID;
You need specify the column name with tablename from which table you want to display or insert data whatever you want :
INSERT INTO cv(expertID)
SELECT e.expertID
FROM experts e INNER JOIN
cv
ON e.expertID = cv.expertID;
try this!
I have used table aliases and then explicitly mentioned the alias with column in select list. Now expertID in select is from experts table rather than ambiguous one.
INSERT INTO cv(expertID)
SELECT e.expertID
FROM experts e
INNER JOIN cv c ON e .expertID = c.expertID;

MySql convert nested SELECT IN queries into JOINs

I read that using IN in MySql queries causes a slowdown in performance, unlike in Oracle and that a JOIN should be used instead. I am able to convert simple queries, but I am struggling with more complex ones that contain nested SELECTs. For example:
update Records set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and LastUpdated!=&
and Pk in (select RecordPk from GroupRecordLink
where GroupPk in (select Pk from Groups where ActiveStatus!=5 and
DateTimeStamp>&))
I had a go rewriting it by following this post , but I am not sure my result is correct.
update Records r join (select distinct RecordPk from GroupRecordLink grl join
Groups g on grl.Pk = g.Pk where g.ActiveStatus!=5 and g.DateTimeStamp>&) s
using (Pk) set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and DateTimeStamp>&
Thanks
Try this:
UPDATE Records
INNER JOIN GroupRecordLink ON Records.Pk = GroupRecordLink.RecordPk
INNER JOIN Groups ON GroupRecordLink.GroupPk = Groups.Pk AND Groups.ActiveStatus != 5
SET Records.ActiveStatus = 0,
Records.TransactionStatus = 0,
Records.LastUpdated = &
WHERE Records.ActiveStatus != 5
AND Records.LastUpdated != &;
In Mysql you could use an explicit join for update (you must change & witha proper value )
update Records
INNER JOIN GroupRecordLink on GroupRecordLink.RecordPk = Records.PK
INNER JOIN Groups on ( GroupRecordLink.GroupPk = Groups.Pk
and Groups.ActiveStatus!=5
and Groups.DateTimeStamp>&)
set ActiveStatus=0,
TransactionStatus=0,
LastUpdated=&

Copy one column to another in different table mysql

I write following query but it is not working it shows Query interrupted
update media m ,cities c
set m.latitude=c.latitude
where m.cities_id=c.id;
anyone can know what is mistake in above query???
There is an alternate syntax that allow for joins in an update but this is the standard SQL way. Make sure the inner query only returns a single value.
update media
set latitude = (
select c.latitude from cities c where c.id = media.cities_id
)
Try this query :
UPDATE media m LEFT JOIN cities c on c.cities_id = m.id SET m.latitude=c.latitude;
Note: ensure Both cities_id , id are of same datatype.
UPDATE media m
INNER JOIN cities c ON m.cities_id=c.id
SET m.latitude=c.latitude;

Update data from one table to another

I have a small problem updating rows of one table with data from another one, please help.
Table l with columns
Make
Model
OEMNumberLatest
OemNumberPrevious
cStockCode
cDescription
The two columns cStockCode,cDescription are blank at the moment and are waiting to be populated, and the second table c with columns StockCode and Description
The question is how to move/copy contents of table c into rows of table l?
Where c.StockCode = l.OEMNumberLatest and or c.StockCode = l.OemNumberPrevious
You probably shouldn't move the contents of one table into the other. It is really better to just get the values when you need them, using a join:
select l.*, c.cStockCode, c.cDescription
from table1 l join
c
on c.StockCode = l.OEMNumberLatest or c.StockCode = l.OemNumberPrevious;
This seems like a strange condition, with the or because you can get multiple matches.
In any case, you can convert this to an update easily:
update table1 l join
c
on c.StockCode = l.OEMNumberLatest or c.StockCode = l.OemNumberPrevious
set l.cStockCode = c.cStockCode,
l.cDescription = c.cDescription;
When both conditions match, then one will arbitrarily be used for the update.
This is a example of a update syntax using another table
UPDATE tableL SET
tableL.cStockCode=tableC.StockCode,tableL.cDescription=tableC.Description
FROM tableL
JOIN tableC ON
(tableC.StockCode = tableL.OEMNumberLatest)
OR
(tableC.StockCode = tableL.OemNumberPrevious);
I assument that the contents in tableC is to be filled in tableL , and the JOIN is based on StockCode with OEMNumberLatest or OemNumberPrevious
Try this (save your data before trying, I am not sure of your database structure) :
UPDATE l
SET l.cStockCode = c.StockCode, l.cDescription = c.Description
FROM l
INNER JOIN c ON (c.StockCode = l.OEMNumberLatest OR c.StockCode = l.OemNumberPrevious)