I need to make some calculation in MySQL table which contains hundreds of rows.
I've got a field called "VAT-Code" which contain a code : 1 or 2
I've got a field called "Price" which contain the price
Query should do the following :
If "VAT-Code" value = 1 calculate "Price" / 1,08 -> update the field value
If "VAT-Code" value = 2 calculate "Price" / 1,025 -> update the field value
Ok I have tried this :
I have tried this :
UPDATE jos_tempcsv SET selling price = CASE `VAT-Code` WHEN = 1 THEN `selling price`/1.08 WHEN = 2 THEN `selling price`/1.025 ELSE NULL END
selling price is the field with the price
But I get the following error :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'price = CASE VAT-Code WHEN = 1 THEN selling price/1.08 WHEN = 2 THEN `sellin' at line 1
You can run UPDATE with CASE:
UPDATE yourtable SET yourfield=CASE `VAT-Code` WHEN 1 THEN `Price`/1.08 WHEN 2 THEN `Price`/1.025 ELSE NULL END;
Or you can run this as two separate UPDATEs:
UPDATE yourtable SET yourfield=`Price`/1.08 WHERE `VAT-Code`=1;
UPDATE yourtable SET yourfield=`Price`/1.025 WHERE `VAT-Code`=2;
You can try the below sql query :
UPDATE yourtable table1,(SELECT * FROM `yourtable` WHERE VAT-Code = 1) table2 SET table1.yourfield = (table2.price/1.08) WHERE table1.VAT-Code = 1
UPDATE yourtable table1,(SELECT * FROM `yourtable` WHERE VAT-Code = 2) table2 SET table1.yourfield = (table2.price/1.025) WHERE table1.VAT-Code = 1
This query works fine now :
UPDATE jos_tempcsv SET `selling price` = `selling price`/1.08 WHERE `VAT-Code`=1;
UPDATE jos_tempcsv SET `selling price` = `selling price`/1.025 WHERE `VAT-Code`=2;
Two queries in one
UPDATE jos_tempcsv SET `selling price` = CASE `VAT-Code` WHEN 1 THEN `selling price`/1.08 WHEN 2 THEN `selling price`/1.025 ELSE NULL END;
Related
Is there a way to combine these sql statement
UPDATE foreign_users_to_be_mentioned SET is_used = 1 WHERE id = 1
UPDATE foreign_users_to_be_mentioned SET is_used = 1 WHERE id = 2
into a single query ?
Use IN:
UPDATE foreign_users_to_be_mentioned SET is_used = 1 WHERE id IN (1, 2)
I have lost hours on this and nothing works for me.
I have filed strp_aa that is default NULL. The filed strp_aa should update only if its null with MAX strp_aa + 1, and if its not null if it already has a number it should stay the same.
short version of code is
UPDATE STRANKEP
SET strp_aa = IF(strp_aa=null, strp_aa+1, strp_aa)
WHERE strp_ID=36;
Also tired
UPDATE STRANKEP
SET strp_aa = IF(strp_aa=null, (SELECT MAX(strp_aa)) +1, (SELECT (strp_aa) WHERE strp_ID=36)
WHERE strp_ID=36;
I tried multiple things like this one mentioned here Update MySQL with if condition:
UPDATE STRANKEP
SET strp_aa = CASE WHEN strp_aa = NULL THEN MAX(strp_aa) + 1 ELSE strp_aa END
WHERE strp_ID = 36;
I have also found this mysql query to update field to max(field) + 1 and tried all sorts of combinations with supplied answer and it wont work for me. One of the version is:
UPDATE STRANKEP
SET strp_aa = IF((SELECT strp_aa )!=null,((SELECT selected_value2 FROM (SELECT (strp_aa) AS selected_value2 FROM STRANKEP WHERE strp_ID=36) AS sub_selected_value2)), ((SELECT selected_value FROM (SELECT MAX(strp_aa) AS selected_value FROM STRANKEP) AS sub_selected_value) + 1) )
WHERE strp_ID=36;
This just keep adding one even if there is a number set on strp_aa...
I don't know what else to try.
EDIT:
Had s little problem with #GMB answer because all fields are starting with NULL, so max(strp_aa) gives 0 results in case none of the fields had a number in it.
I solved that with COALESCE statement and posting it here if someone has similar problem.
UPDATE STRANKEP t
CROSS JOIN (select COALESCE(MAX(strp_aa),0) max_strp_aa from STRANKEP) m
set t.strp_aa = m.max_strp_aa + 1
where t.strp_ID = 36 and t.strp_aa is null
You can use the update ... join syntax for this:
update strankep s
cross join (select max(strp_aa) max_strp_aa from strankep) m
set s.strp_aa = m.max_strp_aa + 1
where s.strp_id = 36 and s.strp_aa is null
The cross join brings the max value of strp_aa over the whole table. The where clause eliminate row(s) where strp_aa is not null.
How can the following be performed in a single query?
UPDATE clients SET online=0 WHERE id NOT IN(4,5,8,10,12) AND id>=2 AND id<=15 AND parentId=123;
UPDATE clients SET online=1 WHERE id IN(4,5,8,10,12) AND id>=2 AND id<=15 AND parentId=123;
You can use CASE .. WHEN statement:
UPDATE clients
SET online = CASE WHEN id IN(4,5,8,10,12)
THEN 1
ELSE 0
END
WHERE
id BETWEEN 2 AND 15 AND parentId = 123;
IN(..) is a Logical/Comparison function. So you can do the following (in MySQL only) as well:
UPDATE clients
SET online = (id IN(4,5,8,10,12))
WHERE
id BETWEEN 2 AND 15 AND parentId = 123;
I am selecting engineStatus field from car table, the query looks like so :-
select engineStatus from car LIMIT 1
car table column can have values 1 or 0.
Assuming engineStatus is set to 1, the above query will return 1.
I want to return ON if engineStatus = 1 or return OFF when engineStatus = 0
I've tried below query, but SQL throws an Error
SELECT ( case engineStatus = 1 then SET engineStatus = 'ON' else SET status = 'OFF') FROM `car` WHERE 1
Mysql Says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'then SET engineStatus = 'ON' else SET engineStatus = 'OFF') FROM `car` WHERE 1 LI' at line 1
How can i achieve this in mysql ?
You miss WHEN and there's no need SET here
The proper syntax is :
SELECT
( CASE
WHEN engineStatus = 1 THEN 'ON'
ELSE 'OFF'
END
) AS status
FROM `car`
WHERE 1
It should be:
SELECT ( case WHEN engineStatus = 1 then 'ON' else
'OFF' END) FROM `car`;
I'm trying to update supplier table on both PostgreSQL and MySQL using the following update statement:
UPDATE SUPPLIERS SET CURDEBT = CURDEBT + 10 WHERE ID = 5
This works fine as long as the CURDEBT column not equals null, if it is null it won't update the record. Does any body have a solution to this problem?
Thanks
In SQL, NULL is not the same thing as 0. Any operations on a NULL value still yield a NULL result. NULL + 10 is still NULL.
If you want NULL to automatically turn into "0" in this query, try this (PostgreSQL):
UPDATE SUPPLIERS SET CURDEBT = coalesce(CURDEBT, 0) + 10 WHERE ID = 5
Or MySQL:
UPDATE SUPPLIERS SET CURDEBT = ifnull(CURDEBT, 0) + 10 WHERE ID = 5
Use coalesce
UPDATE SUPPLIERS SET CURDEBT = coalesce(CURDEBT,0) + 10 WHERE ID = 5
See sqlbook
What you're looking for is COALESCE:
UPDATE SUPPLIERS
SET CURDEBT = COALESCE(CURDEBT, 0) + 10
WHERE ID = 5
Coalesce (MySQL):
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
That behaviour is exactly what you should expect from SQL, since null + x = null, always.
You can solve it by using the COALESCE function, available in both postgres and mysql, like so:
UPDATE SUPPLIERS SET CURDEBT = COALESCE(CURDEBT,0) + 10 WHERE ID = 5