I am creating a MYSQL case, where i want to say:
if age in fieldname "age" = over 20 years old
then update the field "points" to 15 (example value)
It does not accept the "update" statement after the then. Is there another way to do it?
It is a "case" statement from dropdown boxes, so i cannot put too many "if else" statements.
Fieldnames are age, score:
SELECT `FieldName`, `FieldValue`,
CASE WHEN `FieldValue` = "over 20 years old"
THEN (UPDATE `table`
SET `FieldValue`= 15
WHERE `FieldName` = 'points')
When ...(if age less than 20, update points to 10)...
ELSE something else
END
FROM table where `FieldName` = 'age'
UPDATE,
Something is missing, it runs but not updating the prices:
UPDATE `TABLE` SET `FieldValue` = CASE
WHEN `FieldValue` = 'over 20' THEN 5
When `FieldValue` = 'less than 20' THEN 10
ELSE `FieldValue` = 3
END WHERE`FieldName` = 'points'
try this:
UPDATE table SET points=IF(age>20,15,10)
Related
I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.
Take a look at the following query. How do you display Column 'Action' as text.
If the result of 'Action' is LEQ 0 then dipslay the text "Crash" and if 'Action'
is GRT 0 display the text "Hold"?
SELECT col1 AS Action
FROM vdk
WHERE t_stamp Between "{StartTime}" AND "{EndTime}"
Refactoring the answer above, since i don't see the necessity to add a query to an alias table. I think this should work the other answer should work too btw, but its a little more complicated query for no given reason.
SELECT (CASE WHEN col1 <= 0 THEN 'Crash' ELSE 'Hold' END) AS Action
FROM vdk
WHERE t_stamp Between "{StartTime}" AND "{EndTime}"
Use CASE WHEN ... ELSE ... END and select from your set (query):
SELECT *, (CASE WHEN Action <= 0 THEN 'Crash' ELSE 'Hold' END) as ActionText
FROM (
SELECT col1 AS Action
FROM vdk
WHERE t_stamp Between "{StartTime}" AND "{EndTime}"
) q
This application is similar to my first question and I thought it might help someone else down the the road.
User can select from a Table's Drop Down List a set of options to enter a value into the Database.
Using Ignition's Power Table Component's Extension Function configureEditor with the following script.
This script sets up the Drop Down List.
if colName == 'Action':
return {options': [(0, 'Null'), (1, 'HOLD'), (2, 'CRASH')]}
Along with the same Power Table's Extension Function onCellEdited script.
This script enters the selection as a value into the database.
#onCellEdited Upadte Query
row = rowIndex
col = colIndex
colName = colName
value = newValue
ndx = self.data.getValueAt(row,0)
query = "UPDATE vdk SET %s = ? WHERE ndx = ?" % colName
system.db.runPrepUpdate(query,[value,ndx],'history')
system.db.refresh(self.data)
This question already has answers here:
IF statement inside where clause in SQL
(6 answers)
Closed 8 years ago.
I want to use If Else (or similar case determining tech:) under filterring clause of WHERE.
Here is my sample code.
select * from some_table
where
record_valid_flg = 1 and
if(user_param = '1')
then age > 20
else
name like 'Mr%'
end if
How can I figure it out. I would really appreciated any suggestions.. Thanks!
Use CASE like this:
select * from some_table
where
record_valid_flg = 1
and CASE
WHEN user_param = '1' THEN age > 20
ELSE name like 'Mr%'
END;
CASE WHEN user_param = '1' THEN age > 20 ELSE name like 'Mr%' END
Try using the CASE;
SELECT * from some_table
WHERE record_valid_flg = 1
AND
CASE
WHEN (user_param = '1') THEN age > 20
ELSE name like 'Mr%'
END
You can simply do it like this
SELECT
*
FROM some_table
WHERE record_valid_flg = 1
AND IF(user_param = '1', age > 20, name LIKE 'Mr%')
I would like to do the following.
Update a field based on the value of another field like
update table set if(fielda=1){fieldb=2 fieldc=3}else{fieldd=2 fielde=3}
I know this is not valid mysql but its the best way for me to describe the problem.
update table set
b = case when a = 1 then 2 else b end,
c = case when a = 1 then 3 else c end,
d = case when a = 1 then d else 2 end,
e = case when a = 1 then e else 3 end
edit
according to your comment try this:
update table set
datefield_a = case when field_a = 1 then now() else datefield_a end,
datefield_b = case when field_a <> 1 then now() else datefield_b end
I think this syntax will achieve the result you attempted to specify.
UPDATE mytable
SET fieldb = CASE WHEN fielda = 1 THEN 2 ELSE fieldb END
, fieldc = CASE WHEN fielda = 1 THEN 3 ELSE fieldc END
, fieldd = CASE WHEN fielda = 1 THEN fieldd ELSE 2 END
, fielde = CASE WHEN fielda = 1 THEN fielde ELSE 3 END
The "trick" here is that we are updating all four columns, but in some "cases", we are assigning the current value of the column back to the column, resulting in no real change to the column value. (Once you get your mind bent around that idea, it's pretty easy.)
With MySQL, we do have a handy IF function (not available in most other RDBMS) that we can use to abbreviate that a bit, and achieve the same thing:
UPDATE mytable
SET fieldb = IF(fielda = 1, 2, fieldb)
, fieldc = IF(fielda = 1, 3, fieldc)
, fieldd = IF(fielda = 1, fieldd, 2)
, fielde = IF(fielda = 1, fielde, 3)
The pain is that you still have to repeat that same conditional test multiple times.
A single scan through the table (like these statements do), and getting all those assignments done in one fell swoop is going to be faster (and more efficient) than breaking this up and doing the assignments piecemeal using multiple statements.
I am trying to use a mysql case query to update multiple rows in a table. I have some cases which need to update the row with the same value. I was wondering whether it is possible to put all of these into one case or whether I have to create a new 'WHEN' for each?
Below is an example of what I am trying to accomplish but it obviously isn't the correct way to do this because I get an error.
UPDATE `groups` SET `status` = CASE `group_id`
WHEN 32 OR WHEN 33 THEN '1'
WHEN 31 THEN '2'
END
Is it possible to do something like that?
Thanks
I think you want
UPDATE groups SET status = CASE
WHEN group_id = 32 OR group_id = 33 THEN '1'
WHEN group_id = 31 THEN '2'
END
Edit You can use operators like BETWEEN. For example
UPDATE groups SET status = CASE
WHEN group_id BETWEEN 32 AND 33 THEN '1'
WHEN group_id BETWEEN 30 AND 31 THEN '2'
END
TRY
UPDATE `tablename` SET `status`= IF('group_id=31',2,1)
EDIT
UPDATE tableName SET `status` = IF( group_id IN (31, 32), 2, 1 ) WHERE section_id=1
OR
UPDATE tableName SET `status` = IF( group_id ANY (31, 32), 2, 1 ) WHERE section_id=1
running successfully on my table..what error u facing??
other syntax
UPDATE `tableName` SET `group_id` = CASE
WHEN group_id IN (31,32) THEN 1
WHEN group_id IN (33,34) THEN 2
END