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;
Related
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
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
I have two tables, communication(that has a column timestamp,FromIDNumber and FromX ) and commLocation(that has a column timestamp,FromIDNumber and x).
I want to set communication.FromX = commLocation.x when the two tables have the same timestamp and FromIDNumber.
I tried doing this:
UPDATE communication
SET FromX=(SELECT commLocation.x
FROM commLocation
JOIN communication
ON communication.Timestamp=commLocation.timestamp
WHERE communication.FromIDNumber=commLocation.FromIDNumber);
But I got an error:
You can't specify target table 'communication' for update in FROM clause
How would I accomplish this? This is the SQL Fiddle for it. Right now, I'm inserting the FromX values at the end of the table, and I don't want that because I need it to correspond to a certain row of communication.... how would I do this?
Any help would be greatly appreciated, thanks!!
You can do a JOIN operation in an UPDATE statement.
For example:
UPDATE communication c
JOIN commLocation l
ON l.timestamp = c.timestamp
AND l.fromidnumber = c.fromidnumber
SET c.fromx = l.x
This assumes that (timestamp,fromidnumber) is unique in commLocation.
For developing a query like this, we usually start with a SELECT statement...
SELECT c.timestamp
, c.fromidnumber
, c.fromx AS old_fromx
, l.x AS new_fromx
FROM communication c
JOIN commLocation l
ON l.timestamp = c.timestamp
AND l.fromidnumber = c.fromidnumber
The new_fromx in the SELECT is the expression/value we're going to assign to the column. We can return additional columns in the SELECT list, to verify the statement is doing what we want it to do.
Once we get that working, we convert that into an update by removing the SELECT ... FROM and replacing it with UPDATE. And adding a SET clause before the WHERE clause.
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)
I know it's a confusing title, but the example will show what I want to achieve:
UPDATE tr.tbl_to_be_updated SET cat=-1 WHERE cat NOT IN (SELECT c.id
FROM tr.cat as c WHERE c.sh=tbl_to_be_updated.sh))
How can I achieve this? With this query I receive an error that it's not allowed.
UPDATE tbl_to_be_updated
LEFT JOIN cat ON cat.sh = tbl_to_be_updated.sh
SET tbl_to_be_updated.cat = -1
WHERE cat.sh IS NULL