I want to update a table in MySQL like this:
UPDATE Table
SET A = '20' IF A > 20
SET A = A IF A < 20
SET A = 0 IF A <= 1
WHERE A IS NOT NULL;
But the above SQL is not valid Syntax. I also tried this:
UPDATE table
SET A = IF(A > 20, 20, IF(A < 20, A, 0));
But is also invalid Syntax. How do I use an if statement in an update query like this?
I think you were 99% there:
UPDATE table
SET A = IF(A > 20, 20, IF(A < 20 && A > 1, A, 0))
WHERE A IS NOT NULL;
Add the && A > 1 to the second IF statement and your third condition is satisfied.
Edit:
Per #Andre's comment to the question and the suggestion that the nested IF is difficult to read, you could also do this as a couple of queries that don't do any unnecessary work and are readable:
UPDATE table SET A = 20 WHERE A > 20;
UPDATE table SET A = 0 WHERE A <= 1;
When A is NULL, it will not meet either of these conditions, and thus eliminates the need to specify that A not be NULL.
Next, there's no need for the third condition as #Andre suggested. If A is between 1 and 20, it gets left as-is.
Finally, setting A to 0 where A is less than or equal to 1 seems unusual. Values of 1 will be changed to 0. If you intend to simply set values less than 1 (including negative values) to 0, then you should swap < for <=.
UPDATE Table
SET A = Case
When A > 20 Then 20
When A <= 1 Then 0
End
WHERE A IS NOT NULL and ( A > 20 or A <= 1 )
or more simply, 2 statements
UPDATE Table
SET A = 20
where A > 20;
UPDATE Table
SET A = 0
where A <= 1;
With the help of below mentioned query you can update salary field based on conditions. It's a single line update query to update multiple rows in a table based on conditions.
UPDATE table
SET salary = Case
WHEN designation='developer' THEN salary + salary*0.20
WHEN designation='manager' THEN salary + salary*0.30
END
Related
I got the following table and I need to return 1 if all rows have disponibilidad = 1
The following QUERY works just fine, but i was looking for a more efficient way of doing it.
QUERY:
SELECT IF(AVG(disponibilidad) < 1, 0, 1) AS newResult
FROM pasteleria.compone
RIGHT JOIN pasteleria.ingredientes
ON pasteleria.compone.id_ingrediente = pasteleria.ingredientes.id_ingrediente
WHERE id_componente = 1;
RESULT:
As I see it, with an 'AND' it would be far more efficient, since it wouldn't have to do AVG().
MySql does not support a boolean AND aggregate function like Postgresql's bool_and.
Why not a simple MIN():
SELECT MIN(disponibilidad) AS newResult
FROM pasteleria.compone
RIGHT JOIN pasteleria.ingredientes
ON pasteleria.compone.id_ingrediente = pasteleria.ingredientes.id_ingrediente
WHERE id_componente = 1;
This will return 1 only if all values of the column are 1 (provided the column is not nullable) and 0 if there is at least one row with 0.
How about something like
SELECT IF(COUNT(*)>0,0,1) AS newResult
FROM pasteleria.compone
RIGHT JOIN pasteleria.ingredientes
ON pasteleria.compone.id_ingrediente = pasteleria.ingredientes.id_ingrediente
WHERE id_componente = 1
AND disponibilidad <> 1
so that if there are any rows where disponibilidad is not 1, you output 0, otherwise if it's zero (so all disponibilidad values are 1) you output 1?
Is there a way to alter this query so that it would check if frp_fundraisingprogram.start_date is not empty or contains the default value?
SELECT frp_fundraisingprogram.id AS id
FROM frp_fundraisingprogram
WHERE frp_fundraisingprogram.start_date <= DATE_ADD(UTC_TIMESTAMP(), INTERVAL - 2 day)
AND frp_fundraisingprogram.date_entered > '2015-04-09 16:55:18'
AND NOT EXISTS
(SELECT * FROM aow_processed
WHERE aow_processed.aow_workflow_id='9bc1bb2e-cd5a-5c75-cc68-5526ae30331e'
AND aow_processed.parent_id=frp_fundraisingprogram.id
AND aow_processed.status = 'Complete' AND aow_processed.deleted = 0)
AND frp_fundraisingprogram.deleted = 0
Just include that in your where clause...
`...WHERE not isnull(frp_fundraisingprogram.start_date)
AND frp_fundraisingprogram.start_date != '0000-00-00 ...`
That is if you are trying to exclude these records. You can reverse the logic if you only want these records to show up.
I know how to increase/decrease value in column.
value type is INT(11)
Command:
UPDATE table SET value = value - 1 WHERE id = 1
But when value reaches "0" it keeps decreasing.
Question: How to decrease until zero, so I don't get value (-1) in column?
Thank you for your code / ideas / suggestions
Just use a conditional:
UPDATE table
SET value = value - 1
WHERE id = 1 AND value > 0;
Add it to the where clause:
UPDATE table SET value = value - 1 WHERE (id = 1) AND (value > 0)
Once value reaches 0, it won't get updated anymore, because the where clause will exclude that record from the update.
You can add on to your where clause like so:
UPDATE table
SET value = value - 1
WHERE id = 1 and value >= 1
You can use CASE.
UPDATE table SET value = CASE WHEN value = 1 THEN value = value - 1
ELSE value
END
WHERE id = 1
seems like a stupid question...
I have a mysql table where I want to modify column A to a number 0 or 1 depending on the condition of another column B
So: if( B > 500 ) A = 1 ELSE A = 0
Column A = INT
Column B = DOUBLE
How do you do something like this in sql?
Thanks,
Erik
Try the following statement,
UPDATE tableName
SET A = (B > 500)
SQLFiddle Demo
(B > 500) is a boolean arithmetic in mysql which returns 1 and 0 for true and false , respectively.
You can also use CASE for much more RDBMS friendly,
UPDATE tableName
SET A = CASE WHEN B > 500 THEN 1 ELSE 0 END
UPDATE tbl SET counts=counts-1 ...
If count is the only column you're updating (or, you don't have other criteria specified in your where clause), then you can just do that in the where clause
UPDATE [Table] SET counts = counts - 1 WHERE counts > 0;
However, if you're updating other columns in the same query, this won't work. But you have options
UPDATE [Table] SET counts = MAX(counts - 1, 0);
or
UPDATE [Table] SET counts = CASE WHEN counts > 0 THEN counts - 1 ELSE 0 END;
UPDATE tbl
SET counts=counts-1
WHERE counts > 0
thanks to #Peter Bailey
this is the example with the WHERE selector .
UPDATE [tbl_multimedia] SET [m_publish] = CASE WHEN [m_publish] = 0 THEN '1' ELSE '0' END WHERE id='1'
Good luck .