Update multiple tables in one query, in MySQL - mysql

I want update my tables from csv. Now data from csv are imported to table "temp_update_stany", but i cant update tables. Query with no errors, but nothing is updated.
Table from CSV is:
produkt|quantity|price|active|czas
Query:
UPDATE lp2_product tabela
INNER JOIN lp2_stock_available stany ON (tabela.id_product = stany.id_product)
INNER JOIN lp2_product_lang lang ON (tabela.id_product = lang.id_product)
INNER JOIN temp_update_stany csv ON (tabela.id_product = csv.produkt)
SET
tabela.active = csv.active,
tabela.price = csv.price,
lang.available_now = csv.czas,
stany.quantity = csv.quantity
WHERE
csv.produkt = tabela.id_product
OR csv.produkt = lang.id_product
OR csv.produkt = stany.id_product
and output from query:
Modified records: 0 (Perform queries took 0.0322 seconds (s)).
but for example "lp2_product" /row 'active' have value 0 for all products and temp_update_stany have value 1 for all.
Yes, this is prestashop and simple script for update quantity and prices.

As per comments above, the UPDATE reports zero rows affected if there is no net change. So if the tables are already updated with the desired values, the UPDATE is a no-op and no rows are "affected."

Related

MYSQL - join 2 tables, match multiple columns - update value from other table where multiple values match

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.

Update Multiple Tables Using One Data and Query

I'm trying to update multiple tables' rows using one query where thread_id and threadfrn_id are provided.
But it seems like if the row with threadfrn_id = 460 in the files table doesn't exist, then the update does not happen, even for the threads table.
Anyone have a solution to this one?
MySQL
UPDATE threads,files SET threads.del = 1, files.del = 1
WHERE threads.thread_id=460 AND files.threadfrn_id=460
P.S What I'm trying to do is, Update the del column so that I can later delete the rows with del = 1 from the tables.
Include the join in the WHERE clause.
UPDATE threads,files SET threads.del = 1, files.del = 1
WHERE --include join condition for the tables
and threads.thread_id=460 AND files.threadfrn_id=460
Or you can do it in 2 statements
UPDATE threads SET del = 1 where thread_id=460;
UPDATE files SET del = 1 where threadfrn_id=460;
Try below Query:
UPDATE T
SET T.del = 1, OT.del = 1
FROM threads T
INNER JOIN files OT
ON T.thread_id = OT.threadfrn_id
and T.thread_id = 460 and OT.threadfrn_id = 460

Trying to update table with values from second table MySQL results in unending "running query"

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.

Mysql multi-table UPDATE first record

I have a permission system between two objects (users => firms) with table permissions for linking. Now i need to update firms table with first permission user id. I made this query:
UPDATE parim_firms, parim_permissions
SET parim_firms.firm_user_id = parim_permissions.permission_a_id
WHERE parim_firms.firm_user_id = 0
AND parim_firms.firm_id = parim_permissions.permission_b_id
Now if one firm hash multiple linked users, then will it be updated with the first or last matched user?
My logic says after first update firm_user_id != 0 and that row doesn't get updated anymore.
But im not sure, maybe does it run the query for all joined rows and the last row will stay.
And if it doesn't then how can i modify the query to update with only first matched result?
UPDATE parim_firms
SET parim_firms.firm_user_id =
(
select parim_permissions.permission_a_id from parim_permissions
WHERE parim_firms.firm_id = 0
AND parim_firms.firm_id = parim_permissions.permission_b_id
)
or
update parim_firms a
set a.firm_user_id = b.permission_a_id
from parim_permissions b
WHERE parim_firms.firm_id = 0
AND parim_firms.firm_id = parim_permissions.permission_b_id

How to set a column value equal to the value in another table?

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