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
Related
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."
I want to take the below SQL and if there is no update found, I want to add a 0 in that record. Is this possible from a single query? Currently I utilize this query to update records and then go back and run a query to add 0 in null fields.
UPDATE tbl_shortage_report
LEFT JOIN tbl_temp_shortage_report
ON (tbl_shortage_report.Plant = tbl_temp_shortage_report.Plant)
AND (tbl_shortage_report.Material = tbl_temp_shortage_report.Material)
SET tbl_shortage_report.Quantity_Open_Req = [tbl_temp_shortage_report].[Quantity_Open_Req]
WHERE (((tbl_temp_shortage_report.Quantity_Open_Req) Is Not Null));
Something like this may be:
UPDATE tbl_shortage_report
LEFT JOIN tbl_temp_shortage_report
ON (tbl_shortage_report.Plant = tbl_temp_shortage_report.Plant)
AND (tbl_shortage_report.Material = tbl_temp_shortage_report.Material)
SET tbl_shortage_report.Quantity_Open_Req = IFNULL([tbl_temp_shortage_report]. [Quantity_Open_Req],0)
I have two tables: chapter and updates
I have an UPDATE query to set member ids from the updates table into the chapter table. The problem I am trying to find a syntax solution to is that I need to use one query, but have it NOT update (skip) the value if the update value is '0'. When an update is filed (to await UPDATE processing), not all ids are changed, and those that are not are saved into my updates table as '0' while valid changes are a seven digit integer. The problem arises when an UPDATE is applied, any existing ids are overwritten with the '0' when that field should actually have its existing value retained. A sample of my current query is:
UPDATE chapter
SET chapter.election_date = updates.election_date,
chapter.president = updates.president_id,
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE
updates.chapter_id = chapter.id
AND updates.installation_date < CURRENT_DATE ()
Based off of this example, I am trying to find a way to have chapter.president NOT be updated if updates.president_id = '0'
If this is doable, any help or guidance would be appreciated.
Just add this condition to the join clause:
UPDATE chapter
JOIN updates ON updates.chapter_id = chapter.id AND
updates.president_id != '0'
SET chapter.election_date = updates.election_date,
chapter.president = updates.president_id,
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE updates.installation_date < CURRENT_DATE ()
The below query will not update the chapter.president, if updates.president_id=0, but will update all other fields.
UPDATE chapter, updates
SET chapter.election_date = updates.election_date,
chapter.president = if(updates.president_id<>0,updates.president_id,chapter.president)
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE
updates.chapter_id = chapter.id
AND updates.installation_date < CURRENT_DATE()
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 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