Update Multiple Tables Using One Data and Query - mysql

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

Related

how to update rows with same id and also with conditions?

I have a big table (addon_values_storage) where some additional product description is stored.
The column container_id represents the product id. Each product can have multiple entries in this table (addon_values_storage). All rows with the same container_id (which is unique) represent the additional description of one product.
No my problem:
I want to update some description with conditions.
- only update within same container_id
- only update when some information exists within this container_id range
I tried something like this but i think this is not right:
UPDATE addon_values_storage
SET addon_value = "TEXT"
WHERE addon_key = "products_pc_tower_detail"
AND EXISTS (SELECT *
FROM addon_values_storage
WHERE addon_values_storage.container_id = addon_values_storage.container_id
AND addon_values_storage.addon_key = 'products_pc_groupid'
AND addon_values_storage.addon_value = 'CL-AM4-iGPU');
This is how the table looks like:
(picute shows only some rows of container_id = 8, but there are many more 1-1100 container_id's each unique id has about 50 rows...)
Does this achieve what you want?
(it should update addon_values for all entries having "products_pc_tower_detail" as addon_key only if there is another entry in the table with the same container_id and with addon_key = 'products_pc_groupid' and addon_value = 'CL-AM4-iGPU')
UPDATE addon_values_storage avs1
JOIN addon_values_storage avs2
ON avs1.container_id = avs2.container_id
AND avs2.addon_key = 'products_pc_groupid'
AND avs2.addon_value = 'CL-AM4-iGPU'
SET avs1.addon_value = "TEXT"
WHERE avs1.addon_key = "products_pc_tower_detail"
Edit: fixed the typo :)
With a self join and the conditions in the WHERE clause:
update addon_values_storage t1
inner join addons_values_storage t2 on t1.container_id = t2.container_id
set t1.addon_value = "TEXT"
where
t1.addon_key = "products_pc_tower_detail"
and t2.addon_key = 'products_pc_groupid'
and t2.addon_value = 'CL-AM4-iGPU'

Mysql Can I make an update with join

I have a database with several tables and all my queries work but I would like to had a new one and can't succeed to make it working.
Two tables 'vins' and 'producteurs'with a foreign key IDproducteur in my 'vins' table.
I need to update the 'vins'table from an excel source where the data are not always written corectly and need to change the stock value for some records of the 'vins' table.The p.nomproducteur and V.annee and v.nom are parameters but for test I put hard values.
My Query is the following:
UPDATE vins AS v
JOIN producteurs p
ON ( p.IDproducteur = v.IDproducteur AND p.nomproducteur LIKE "Charles" )
SET stock = 10
WHERE v.nom LIKE "Symphonie" AND v.annee = 2013
I have two records where the producteur is "Charles" and V.nom is Symphonie one withe annee = 2010 and one with 2013. I got no error in phpMyadmin but there is no results to my Query even by changing some command order.
Is what I want impossible to do?.
Put the condition p.nomproducteur LIKE "Charles" in the WHERE clause:
UPDATE vins v
JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET stock = 10
WHERE
v.nom = "Symphonie" AND v.annee = 2013 AND p.nomproducteur = "Charles"
Also there is no need for LIKE in this case, a simple = will do.
The update based on JOIN is commonly used in mysql so be sure that your join and where condition really match the values in your tables
UPDATE vins AS v
INNER JOIN producteurs p ON p.IDproducteur = v.IDproducteur
SET v.stock = 10
WHERE v.nom = "Symphonie"
AND v.annee = 2013
AND p.nomproducteur = "Charles"
and do the fact you are not filter for partial string use = instead of like

Update multiple tables in one query, in 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."

Mixed UPDATE SELECT query

I need to run an UPDATE query:
UPDATE products
SET fcategory = SELECT fcategory
FROM categories
WHERE categories.scategory = products.scategory
i.e. for the rows in "products" where column scategory = categories.scategory, products.fcategory must be updated to categories.fcategory
Example:
categories.scategory monkies, categories.category gorilla
products.scategory monkies
=> products.fcategory must be updated to gorilla since products.scategory = monkies = products.scategory
Anyone knows how to write such an UPDATE query?
Thanks.
Use a JOIN
UPDATE products AS p
JOIN categories AS c ON p.scategory = c.scategory
SET p.fcategory = c.scategory

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