Update Field When Not Null - mysql

I have an update statement that updates fields x, y and z where id = xx.
In the table I have a few different x_created_datetime fields (for different portions of the record that are maintained/entered by different folks). I'd like to write a single query that will update this field if is null, but leave it alone if is not null.
So what I have is:
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp"
WHERE id = X
What I need is a way to add in the following, but still always update the above:
scan_created_date = "current_unix_timestamp"
where scan_created_date is null
I'm hoping I can do this without a second transaction to the DB. Any ideas on how to accomplish this?

Do this:
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp",
scan_created_date = COALESCE(scan_created_date, "current_unix_timestamp")
WHERE id = X
The COALESCE function picks the first non-null value. In this case, it will update the datestamp scan_created_date to be the same value if it exists, else it will take whatever you replace "current_unix_timestamp" with.

mySQL has an IFNULL function, so you could do:
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp"
scan_created_date = IFNULL( scan_created_date, "current_unix_timestamp" )
WHERE id = X

I think that what you're looking for is IF()
UPDATE newspapers
SET scan_notes = "data",
scan_entered_by = "some_name",
scan_modified_date = "current_unix_timestamp",
scan_created_date = IF(scan_created_date IS NOT NULL, "current_unix_timestamp", NULL)
WHERE id = X

You could use COALESCE() wich returns the first NON-NULL value):
scan_created_date = COALESCE(scan_created_date, "current_unix_timestamp")

You can do something like this:
UPDATE newspapers a, newspapers b
SET a.scan_notes = "data",
a.scan_entered_by = "some_name",
a.scan_modified_date = "current_unix_timestamp",
b.scan_created_date = "current_unix_timestamp"
WHERE a.id = X AND b.id = X AND b.scan_created_date is not NULL

Its like equivalent to Oracle's NVL.
You can use it like below in a prepared statement using parameters
UPDATE
tbl_cccustomerinfo
SET
customerAddress = COALESCE(?,customerAddress),
customerName = COALESCE(?,customerName),
description = COALESCE(?,description)
WHERE
contactNumber=?

Related

Update Statement in oracle PL/SQL updating the values to null

UPDATE FD DCS
SET DCS.F_DISTANCE =
(SELECT FD.F_DISTANCE
FROM TMP FD
WHERE FD.DATE_SID = DCS.DATE_SID
AND FD.SID = DCS.SID
AND FD.DA_SID = DCS.DASID
AND FD.AA_SID = DCS.AA
AND FD.F_DISTANCE IS NOT NULL
)
WHERE DCS.BATCH_ID=BATCH_ID;
The inner SQl query is updating all the satisfied values to the FD table's column F_distance but where subquery didn't match it is updating the FD table F_Distance values to null. I don't want to update the values to null.
please suggest what to do.
In ORACLE you can use NVL
UPDATE FD DCS
SET DCS.F_DISTANCE = NVL(
(SELECT FD.F_DISTANCE
FROM TMP FD
WHERE FD.DATE_SID = DCS.DATE_SID
AND FD.SID = DCS.SID
AND FD.DA_SID = DCS.DASID
AND FD.AA_SID = DCS.AA
AND FD.F_DISTANCE IS NOT NULL
), DCS.F_DISTANCE)
WHERE DCS.BATCH_ID=BATCH_ID;
In this case if the result is null the NVL return 0 (you cna change with the value you prefer)

Mysql, change (update) all numeric values for all table

source:
constant x,y,
mysql table named 'table1'. It contains column pp.
Need change all values in table1:
pp_new = pp/x*y;
pp_new = round(pp_new) to 0.1.
For example 12.31 = 12.30, 14.56 = 14.50, 55.1245035 = 55.10
12.31 = 12.30, 14.56 = 14.50, 55.1245035 = 55.10
You need to use truncate(number,decimalplaces)
TRUNCATE(1.25864,1)
this will return 1.2
What about something like UPDATE table1 SET pp = round(pp/x*y) ?

How do I update a table with fields selected from another table?

Although there are many questions similar to this, such as
"Updating a record from another table", but i could not get this working.
I have a query that selects and updates table sem_stdexamfinresmark. The select subquery returns multiple rows of data whose size may not be equal to the table being updated, but the update is now working.
The query looks like :
update sem_stdexamfinresmark sr,
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession =1 and classid='8'
) s
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession = s.currsession and
sr.classid='8';
EDIT:
update sem_stdexamfinresmark
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
from
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession = 1 and classid='8'
) s
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession =1 and
sr.classid='8';
select * from sem_exam;
update sem_exam set currsession =1;
try something that looks more like:
update foo
set col = bar.col
from bar
where ...
This is what happens when one loses sleep :( I just did a silly mistake here and added "and"

SQL Join only on condition?

I have about 10 different sql statements that update different tables. They look similar to this:
Update Y
SET x = n
Where something = #somevar
Now I need to Update only certain rows when the #hasRows var is set. I could simply do this:
if not #hasRows is null
begin
Update Y
SET x = n
from Y inner join #items on y.Item = #items.Item
Where something = #somevar
end
else
begin
Update Y
SET x = n
Where something = #somevar
end
Is there a way to avoid the if/else and do the update in one statement?
I am using SQL2005.
Perhaps something like this: (copypasta your example)
UPDATE Y
SET x = n
FROM Y
WHERE something = #somevar
AND (
(#Items IS NULL)
OR (y.Item = #Item)
)
The JOIN isn't used but it's always proceeding if #items is NULL, or using the intended condition.
Problem here is that your example seems to include a TVP #ITEMS but TVPs don't exist in SQLServer2k5? So whatever the value is should be placed in the parameter.
Alternatively, if #Table exists but has no rows you can do this:
UPDATE Y
SET x = n
FROM Y
JOIN #Items ON (#HasRows = 0) OR (#Items.Item = Y.Item)
WHERE something = #somevar
If you don't know whether or not #Items IS NULL then your condition is your option because declared variables are resolved before the statement is executed.

updating a column with avg data from another table column

i wrote a command like this to update a column in one table with avg of columns from another table.. its giving errors
UPDATE college_rating,products set
property1_avg = avg(college_rating.rating1),
property2_avg = avg(college_rating.rating2),
property3_avg = avg(college_rating.rating3),
property4_avg = avg(college_rating.rating4),
property5_avg = avg(college_rating.rating5),
property6_avg = avg(college_rating.rating6),
property7_avg = avg(college_rating.rating7),
property8_avg = avg(college_rating.rating8),
property9_avg = avg(college_rating.rating9),
property10_avg = avg(college_rating.rating10),
property11_avg = avg(college_rating.rating11),
property12_avg = avg(college_rating.rating12),
property13_avg = avg(college_rating.rating13),
property14_avg = avg(college_rating.rating14),
property15_avg = avg(college_rating.rating15)
where products.alias = concat(college_rating.property1,'-',college_rating.property2,'-',college_rating.property3)
group by college_rating.property1,college_rating.property2, college_rating.property3
The MySQL multi-table update syntax does not allow the use of group by.
You can accomplish what you are trying to do by moving the aggregation into a sub-query and joining to that sub-query in the multi-table-update instead.
Something like this should work:
update products p
inner join (
select concat(property1,'-',property2,'-',property3) as alias,
avg(rating1) as property1_avg,
avg(rating2) as property2_avg,
avg(rating3) as property3_avg,
avg(rating4) as property4_avg,
avg(rating5) as property5_avg,
avg(rating6) as property6_avg,
avg(rating7) as property7_avg,
avg(rating8) as property8_avg,
avg(rating9) as property9_avg,
avg(rating10) as property10_avg,
avg(rating11) as property11_avg,
avg(rating12) as property12_avg,
avg(rating13) as property13_avg,
avg(rating14) as property14_avg,
avg(rating15) as property15_avg
from college_rating
group by property1,property2, property3
) as r on r.alias = p.alias
set p.property1_avg = r.property1_avg,
p.property2_avg = r.property2_avg,
p.property3_avg = r.property3_avg,
p.property4_avg = r.property4_avg,
p.property5_avg = r.property5_avg,
p.property6_avg = r.property6_avg,
p.property7_avg = r.property7_avg,
p.property8_avg = r.property8_avg,
p.property9_avg = r.property9_avg,
p.property10_avg = r.property10_avg,
p.property11_avg = r.property11_avg,
p.property12_avg = r.property12_avg,
p.property13_avg = r.property13_avg,
p.property14_avg = r.property14_avg,
p.property15_avg = r.property15_avg;
What is the error that you get? And you need to have a WHERE clause unless you want the UPDATE query to apply to ALL the records
I think you'd need to use sub queries, and I'm not sure if you can update two tables like that in MySQL, at least not without prefixing the attributes.

Categories