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.
Related
can you help me with an sql query?
I want to get a value from another table but cannot use an inner join because the "join" column is not exactly the same in these two tables. Instead I must check the place where "Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max" holds.
This is my research efford so far:
UPDATE
Table_A
SET
Table_A.clubState = Table_B.state
FROM
clubs_data AS Table_A
JOIN zip_to_state AS Table_B
WHERE
Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max
However it draws an syntax-error on line 5.
Thank you!
Here is your query re-written with a valid syntax:
UPDATE clubs_data
SET clubState = (
SELECT state
FROM zip_to_state
WHERE clubs_data.clubZipCode >= zip_to_state.zip_min AND clubZipCode <= zip_to_state.zip_max
LIMIT 1
);
I do not know what you want to achieve, but generally the update syntax using JOIN would be something like this:
UPDATE clubs_data AS Table_A
JOIN zip_to_state AS Table_B
ON Table_A.clubZipCode >= Table_B.zip_min
AND Table_A.clubZipCode <= Table_B.zip_max
SET Table_A.clubState = Table_B.state;
EDIT: The solution just proposed by #Gab using a subquery might be better suited for you in this case.
I'm trying to write a MYSQL Query that updates a cell in table1 with information gathered from 2 other tables;
The gathering of data from the other 2 tables goes without much issues (it is slow, but that's because one of the 2 tables has 4601537 records in it.. (because all the rows for one report are split in a separate record, meaning that 1 report has more than 200 records)).
The Query that I use to Join the two tables together is:
# First Table, containing Report_ID's: RE
# Table that has to be updated: REGI
# Join Table: JT
SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100
This returns 100 records (I limit it because the database is in use during work hours and I don't want to steal all resources).
However, I want to use these results in a Query that updates the REGI table (which it uses to select the 100 records in the first place).
However, I get the error that I cannot select from the table itself while updateing it (logically). So I tried selecting the select statement above into a temp table and than Update it; however, then I get the issue that I get to much results (logically! I only need 1 result and get 100) however, I'm getting stuck in my own thougts.. I ultimately need to fill the ReportID into each record of REGI.
I know it should be possible, but I'm no expert in MySQL.. is there anybody that can point me into the right direction?
Ps. fixing the table containing 400k records is not an option, it's a program from an external developer and I can only read that database.
The errors I'm talking about are as follows:
Error Code: 1093. You can't specify target table 'TrialTable' for update in FROM clause
When I use:
UPDATE TrialTable SET TrialTable.BlanccoReport =
(SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100)
WHERE TrialTable.HardwareType="PC" AND TrialTable.BlanccoReport=0)
Then I tried:
UPDATE TrialTable SET TrialTable.BlanccoReport = (SELECT ReportID FROM (<<and the rest of the SQL>>> ) as x WHERE X.SerialNo = TrialTable.Serienummer)
but that gave me the following error:
Error Code: 1242. Subquery returns more than 1 row
Haveing the Query above with a LIMIT 1, gives everything the same result
Firstly, your query seems to be functionally identical to the following:
SELECT RE.report_id ReportID
, REGI.Serienummer SerialNo
FROM Blancco_Registration.TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92
LIMIT 100
So, why not use that?
EDIT:
I still don't get it. I can't see what part of the problem the following fails to solve...
UPDATE TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
SET TrialTable.BlanccoReport = RE.report_id
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92;
(This is not an answer, but maybe a pointer towards a few points that need further attention)
Your JT sub query looks suspicious to me:
(SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92
GROUP BY RE.report_id)
You use group by but don't actually use any aggregate functions. The column RE.Value_string should strictly be something like MAX(RE.Value_string) instead.
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;
I am bit stuck with this one.. what i want is update app_name (first table). It returns no error.. but does nothing...
UPDATE tbl_m_app AS tma, tbl_measure AS tm
SET tma.app_name='Ap1'
WHERE (tm.mesure_id = tma.mesure_id
AND tm.live = 1)
This query will do the same work in more obvious way and without joins
UPDATE tbl_m_app AS tma
SET tma.app_name='Ap1'
WHERE tma.mesure_id IN (SELECT tm.mesure_id FROM tbl_measure AS tm WHERE tm.live = 1)
I think this SQL is fine, it's just not matching any rows.
Check this by using a select with the same where clause:
SELECT * FROM tbl_measure tm WHERE tm.live=1;
0 rows returned, right?
I have two separate SELECT statements:
SELECT VCe.VId FROM `VCe` WHERE `YId` = 9007 AND `MaId` =76 AND `MoId` = 2851
SELECT r_pts.p_id FROM r_pts WHERE r_pts.v_id IN (57202, 57203, 69597, 82261, 82260, 69596, 69595, 82259)
When they are run separately they both complete in under .05sec however when I nest the first one within the second, it dramatically increases to 3.3sec.
I would like to do a join so that I can get the output from the second SELECT using the first select as the result set for the IN() but I cannot figure out how to include WHERE conditions in a JOIN.
Edit: Also what is the correct syntax to do a join as I am requesting?
Thanks for your help, its appreciated!
Equivalent to MattMcKnight's query whilst illustrating "how to include WHERE conditions in a JOIN":
SELECT r.p_id
FROM r_pts r
INNER JOIN VCe v
ON v.VId = r.v_id
AND
v.YId = 9007
AND
v.MaId = 76
AND
v.MoId = 2851
SELECT r_pts.p_id FROM r_pts, 'VCe' WHERE r_pts.v_id = VCe.VId AND VCe.YId = 9007 AND VCe.MaId =76 AND VCe.MoId = 2851
The basic goal of a join is to describe how the two tables relate. I inferred from your example that the v_id column in the r_pts table was a foreign key pointing to the VId primary key in the VCe table. When you add a term in the query (such as "r_pts.v_id = VCe.VId") that has a field from each table you wish to join, that tells the database how to match up the rows between the tables to make "virtual rows" that contain the columns from both tables. Your other query terms limit which rows are included in the result set.