MySQL ifnull sub query - mysql

I am trying to update a table and set a column, set_price, if it is null to a value from another table.
Both records share the same prod_id. Can someone look over my query and tell me whats wrong with it?
update list_items l
set purchased = "YES",
set_price = IFNULL(set_price,(select pricelast
from inventory i where i.prod_id=l.prod_id))
where l.list_id=1

A better way would be using join update with the use of case-when something as
update list_items l
left join inventory i on i.prod_id=l.prod_id
set l.purchased = 'YES',
l.set_price =
case
when l.set_price is null then i.pricelast else l.set_price
end

Related

[SQL]How to delete all rows containing NULL value on the joined table column using LEFT JOIN and DELETE

I am trying to get rid of every row that has no relation to the other table.
This is what I tried:
DELETE obj FROM objednavky obj
LEFT JOIN polozky_objednavek poObj
ON poObj.objednavka_id = obj.id
WHERE poObj.objednavka_id = null
DELETE is only working on one table, so it should not be joined. you can use the other table in your WHERE clause though, like this:
DELETE FROM objednavky obj
WHERE( SELECT objednavka_id FROM polozky_objednavek WHERE objednavka_id=objednavky.id) IS NULL
If you're doing this manually I recommend running the SELECT clause first (or at least a COUNT variant) to check you're not going to accidentally wipe out the entire table.
DELETE obj FROM objednavky obj
LEFT JOIN polozky_objednavek poObj
ON poObj.objednavka_id = obj.id
WHERE poObj.objednavka_id IS null
thanks to #forpas for comenting, I just needed to change: item = NULL to item IS NULL and worked itexactly as I needed. Keep in mind that SQL doesn't support this type of comparison.
You can use NOT EXISTS.
Try this:
DELETE FROM objednavky obj
WHERE NOT EXISTS (
SELECT
*
FROM
polozky_objednavek poObj
WHERE
`poObj`.`objednavka_id` = `obj`.`id`);
Demo

SQL Update with 0 if no match

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)

SQL Update Table confusion?

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.

Update query using one of its fields in a select query in where clause

I know it's a confusing title, but the example will show what I want to achieve:
UPDATE tr.tbl_to_be_updated SET cat=-1 WHERE cat NOT IN (SELECT c.id
FROM tr.cat as c WHERE c.sh=tbl_to_be_updated.sh))
How can I achieve this? With this query I receive an error that it's not allowed.
UPDATE tbl_to_be_updated
LEFT JOIN cat ON cat.sh = tbl_to_be_updated.sh
SET tbl_to_be_updated.cat = -1
WHERE cat.sh IS NULL

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