I have a question regarding updating a MySQL database.
I have three tables: Match, Submission and SubmissionVersion. A SubmissionVersion can be set as 'Favorite'. But I can't just query UPDATE SubmissionVersion SET IsFavorite = 1 WHERE ID = $ID because of the relation to a Submission and than the Match. My question is how can I update a SubmissionVersion column with a MySQL Query with two joins? I've tried this query but I can't get it to work.
UPDATE
SubmissionVersion
JOIN
Submission
ON
Submission.ID, SubmissionVersion.SubmissionID
JOIN
Match
ON
Match.ID ON Submission.MatchID
SET
SubmissionVersion.IsFavorite = ".$Index."
WHERE
SubmissionVersion.ID = ".$ID."
AND
Match.ID = ".$MatchID
UPDATE SubmissionVersion sv
SET sv.IsFavorite = ".$Index."
WHERE sv.ID = ".$ID."
AND sv.ID IN (
SELECT s.ID
FROM Submission s
WHERE s.MatchID = ".$MatchID'")
If I understand your statement correctly, that should work.
I eliminated the Match table completely since you're just checking the value against a column in Submission.
Let's start with saying that MATCH is MySQL's reserved word, so needs to be put into backticks.
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
Related
I need to join two tables (TableCorrected) with (TableOriginal). Both tables have columns for ID-number and Articlenumber. Both tables also have a column named "Quantity".
I want to join the two tables, match ID-number AND Articlenumber and update the Quantity-value from "TableOriginal" to "TableCorrected" ONLY where ID-number as well as Articlenumber matches.
I've started a statement as below - but I'm sure it's not correct, returns 0 result.
UPDATE TableOriginal
INNER JOIN TableCorrected ON TableOriginal.ID = TableCorrected.ID
SET TableOriginal.Quantity = TableCorrected.Quantity
WHERE TableCorrected.ID = TableOriginal.ID
AND TableCorrected.Article = TableOriginal.Article
but I'm sure it's not correct
It is correct. But not optimal. More clear is, for example,
UPDATE TableOriginal
INNER JOIN TableCorrected USING (ID, Article)
SET TableOriginal.Quantity = TableCorrected.Quantity;
or
UPDATE TableOriginal
INNER JOIN TableCorrected ON TableOriginal.ID = TableCorrected.ID
AND TableOriginal.Article = TableCorrected.Article
SET TableOriginal.Quantity = TableCorrected.Quantity;
returns 0 result.
UPDATE does not return rows. Rather than SELECT.
Example we have 2 tables:
device_tb has columns "device_num" and "device_name"
property_tb has columns "id" and "item_name(currently null or placeholder values)"
device_tb.device_num is equal to the property_tb.id as in it lists the unique id of the product.
If i want to update property_tb.item_name with the strings from device_name instead of manually keying in the names how would i go about it?
Would this work?
UPDATE property_tb
SET item_name= device_tb.device_name
WHERE property_tb.id = device_tb.device_num
You have to join both the tables before updating. Here is the sample code
UPDATE property_tb
SET property_tb.item_name = device_tb.device_name
FROM property_tb
JOIN device_tb ON property_tb.id = device_tb.device_num
In MySQL, the correct syntax for an UPDATE with JOIN is:
UPDATE property_tb p JOIN
device_tb d
ON p.id = d.device_num
SET p.item_name= d.device_name;
I'm trying to replace null values in one table with values from a second table, based on matches from other columns in both tables. While the code does not result in error, it does not stop running, producing an unending "running query" signal. code is here
UPDATE pl_building b
INNER JOIN pl_grt t
ON b.INST = t.inst
SET b.Utuition=t.tuition
WHERE b.UtUITION = 0;
You should not update on join tables.
I am not sure what field you want to update, but your SQL should look like this:
UPDATE pl_building b
SET b.Utuition= (select t.tuition from pl_grt t ON b.INST = t.inst)
WHERE b.UtUITION = 0;
Make sure :
1) You have an index on t.inst table column and maybe also on b.UtUITION
2) Relationship between b.INST = t.inst is unique. Never returns more than 1 row.
I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"
I am trying to figure out how to update a row in one table, setting a column value equal to a value in a different table. Here's an example:
movies:
movie_id | movie_price
movies_attended:
attended_id | attended_movie_id | attended_movie_price
Now, this is kind of a stupid example, but supposed that for some reason there is a row in movies_attended that does not have the correct attended_movies_price in it and so it needs to be updated.
How should a query be written to update the movies_attended table, setting movies_attended.attended_movie_price = movies.movie_price?
I tried something similar to the following, but it did not work:
update movies_attended, movies
set movies_attended.attended_movie_price = movies.movie_price
where movies_attended.attended_movie_id = movies.movie_id
AND attended_id = [the id of the row we want to update]
When you say "it did not work", do you mean that it reported 0 rows updated, or did the statement cause the database raise an exception?
Your example statement appears to be of the form:
UPDATE movies_attended a
JOIN movies m
ON a.attended_movie_id = m.movie_id
SET a.attended_movie_price = m.movie_price
WHERE a.attended_id = ?
(We typically prefer the JOIN ... ON ... style syntax to the comma join operator and the join predicates in the WHERE clause.)
I have no explanation as to why this statement would "not work".
It's possible this would report 0 rows affected, if no rows satisfy the predicates. It would also report 0 rows affected if the rows that would be changed do not require any changes... that is, the existing value in attended_movie_price already matches the value being assigned to it.
Normally, before running an update statement like that, I write it as a SELECT first, and see what values are returned...
By replacing the UPDATE keyword with SELECT ... FROM, and removing the SET clause:
SELECT m.movie_price AS new_val
, a.attended_movie_price AS old_val
, a.attended_id
FROM UPDATE movies_attended a
JOIN movies m
ON a.attended_movie_id = m.movie_id
WHERE a.attended_id = ?
This is actually a bad database design. You don't need movie price in two tables.
But, if you just need this, it goes something along this:
UPDATE movies_attended
INNER JOIN
movies
ON movies_attended.attended_movie_id = movies.movie_id
SET movies_attended.attended_movie_price = movie.movie_price