toggeling with case when -SQL - mysql

I want to toggle the activate column in my table; I think it can be done with case statement! I have tried following but apparently it does not work and there is a syntax problem; could you please let me know if it can be done with one statement like the following:
update likes l case when active = 1 then set active=0 else set active=1 end where l.uuid=11 and l.scene_id=2;

Try syntax like
update likes SET
active = case when active = 1 then 0 else 1 end
where uuid=11 and scene_id=2;

Since you're using MySql, you could also do the following:
update likes
set active = !active
where uuid=11 and scene_id=2;
SQL Fiddle Demo

Related

Phpmyadmin+codeigniter how can i auto update my data?

please help me i'm trying to make an auto update using phpmyadmin Trigger for my capstone title. is this correct?? i just need only to auto update in specific row only.
SELECT * FROM event
if((start_date==now())||end_date==now())
THEN
UPDATE event
SET
event_status = 'Ongoing'
WHERE
event_id = ????(Problem here);
ELSEIF((start_date < now())&&(end_date>now()))
THEN
(some code)
else if((start_date<now())&&(end_date<now()))
(some code)
ELSE
(some code)
For that you have the pseudo rows NEW and OLD, as the name suggests it has all the columns with the old and new values
if((start_date==now())||end_date==now())
THEN
UPDATE event
SET
event_status = 'Ongoing'
WHERE
event_id = NEW.event_id;
ELSEIF((start_date < now())&&(end_date>now()))
THEN
(some code)
else if((start_date<now())&&(end_date<now()))
(some code)
ELSE
(some code)

SET inside IF in MYSQL(?)

I'm new in MYSQL and I'm trying to do this:
UPDATE team
SET member1 = IF(member1=0, 001, SET member2 = IF(member2=0, 001, member2)) WHERE ID = ?;
This is not working, explain:
Each "member" has a different number (001, 002, 003...), I want to put the member "001" in the table "member1" but if this is ocupated (it has a value different of 0) set the member "001" in the table "member2" (a diferent table) but I think is not possible to put a SET inside of an IF. The id doesn't matters.
Thanks for attention ^^
No you can't join SET statements like this. It'll have to be worked around:
UPDATE `team`
SET
`member2` = CASE WHEN `member1` = 0 AND `member2` = 0 THEN '001' ELSE `member2` END,
`member1` = CASE WHEN `member1` = 0 THEN '001' ELSE `member1` END
WHERE
`id` = ?
/* optional filter: considering adding it if it does not bother the performance: */
AND (`member1` != 0 OR `member2` != 0)
;
Notes:
I used backticks around table and column names, I recommend you do so*
I used CASE/WHEN instead of IF, because IF is not ansi sql (it is specific to mysql)
You could also do two queries instead of one. That would allow you to use a filter in the where clause, so you don't have to update the column when it's not required.
*as pointed out by Bohemian, they are not ansi. I like to use them for the structural and visual help they provide.

case expression to change the value of a column

I have two columns are measurement_unit and measurement_size.The values in measurement_unit column are ml,gm,litre.And measurement_size has the values 200,1300,2000i have converted all values of measurement_size to round figure such as .2,1.3,2 now i want to change the values of measurement_unit to liter or kg and here is my case expression .But it is not working..what should i do?In addition it is not a update query i am doing this for my report..it will not change any value in database..
CASE WHEN MUC.name='ml' THEN (MUC.name)='Liter'
WHEN MUC.name='gm' THEN (MUC.name)='Kg'
END AS Measurement,
One possibility would be to use a CASE WHEN inside an UPDATE statement:
UPDATE MUC
SET MUC.name = CASE
WHEN MUC.name = 'ml' THEN 'Liter';
WHEN MUC.name = 'gm' THEN 'Kg';
ELSE MUC.name
END
The only thing I don't like about this solution is that it will still update a row which does not match, using the current value. If you really only want to update the rows which you intend to convert, then you can try using two UPDATE statements, wrapped in a transaction:
START TRANSACTION
UPDATE MUC
SET MUC.name = 'Liter' WHERE MUC.name = 'ml'
UPDATE MUC
SET MUC.name = 'Kg' WHERE MUC.name = 'gm'
COMMIT
The transaction might be necessary if you want to ensure that no one ever sees your units in an intermediate state.
You can try this.
select
CASE WHEN M_unit='ml' THEN 'Liter'
WHEN M_unit.name='gm' THEN 'Kg'
ELSE M_UNIT
END AS Measurement_UNIT,
M_size/1000 as Measurement_SIZE
from Table

Is it possible to have mulitiple checks in a single mySQL IF case?

I want to write a query that will verify 8 parameters of a row are the same as the parameters passed to the procedure. If any one of them is different then modify a status, otherwise do nothing.
Is there a way I can check all those parameters in a single IF case? For example:
IF (v_duty <> duty) OR (v_current <> current) OR (v_frequency <> frequency) THEN
* UPDATE ......;
END IF
Or do I have to use an ELSE IF for each comparison I want to make?
The above doesn't work, with or without brackets between each test.
You could probably manage this with a simple UPDATE and WHERE condition:
UPDATE table_name
SET status_column = 'new_status'
WHERE identifying_column = :identifier
AND (
v_duty != :v_duty
OR v_current != :current
OR v_frequency != :frequency
)

Basic logic in MySQL

Our current mysql script that connects our Invoicing software to our website updates stock levels and what not, but there is a field in our products table which dictates if the product is visible or not which the script does not address. I want to introduce some IF logic to set the prodvisible column to 1 IF the stock level it's being updated with is > 0.
In my research, it appears that IF's cannot appear outwith functions, sadly something I have no experience of and despite my best efforts I can't get it to work.
The current script we have which works succesfully to update stock levels is as follows...
update isc_products p
set
p.prodcurrentinv =[{Level_LessOrderBook}]
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
--GO;--
update isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where pvc.vcsku='[{ItemNumber}]'
I want to integrate something into the first section that does something like the following
If [{Level_LessOrderBook}] > 0
p.prodvisible = 1 where p.prodcode = '[{ItemNumber}]'
ENDIF
I don't want it to set the product to invisible if it it's out of stock, just visible if it's in stock.
Thanks for any help.
You should be able to do this without an IF statement:
update isc_products p
set p.prodvisible = 1
where p.prodcode = '[{ItemNumber}]'
and [{Level_LessOrderBook}] > 0
Or, if you were asking about doing it in one statement:
update isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = IF([{Level_LessOrderBook}] > 0, 1, p.prodvisible)
where p.prodcode = '[{ItemNumber}]' and p.prodinvtrack=1
Finally, how about this?
update
isc_products p
set
p.prodcurrentinv = [{Level_LessOrderBook}],
p.prodvisible = case when [{Level_LessOrderBook}] > 0 then 1 else p.prodvisible end
where
p.prodcode = '[{ItemNumber}]'
and p.prodinvtrack = 1
--GO;--
update
isc_product_variation_combinations pvc
set
pvc.vcstock = [{Level_LessOrderBook}]
where
pvc.vcsku='[{ItemNumber}]'
I formatted everything in exactly the same way as the rest of your existing script. Are you sure that the column prodvisible exists, is spelled correctly, and takes a numeric or bit value?