Mysql update query with IF statement - mysql

I'm learning to use mysql so i created a really simple project manager program.
i wanted to track if a user saw her/his tasks in a project. So i want to update user_seen_task filed when a project is got queried by a user, but obviously i only want to update this field if it isn't already set.
This is my approach what i have already:
UPDATE task
SET user_seen_task = 1, user_seen_task_date = NOW()
WHERE project_id = 14 AND user = 4 AND user_seen_job != 1
Unfortunately this query will update user_seen_task_date every time it runs.
Any help is greatly appreciated.

UPDATE task
SET user_seen_task = 1
, user_seen_task_date = CASE
WHEN user_seen_task_date IS NULL THEN NOW()
ELSE user_seen_task_date
END
WHERE project_id = 14
AND user = 4
AND user_seen_job != 1
Or if it doesn't need to update user_seen_task in this case, just add
AND user_seen_task_date IS NULL
into your where clause
UPDATE task
SET user_seen_task = 1, user_seen_task_date = NOW()
WHERE project_id = 14 AND user = 4 AND user_seen_job != 1
AND user_seen_task_date IS NULL

Related

Why does UPDATE id_order_state = 4 modifies all the field?

I am new with MYSQL and I am having trouble with this code:
UPDATE gf_order_history
SET date_add =
( SELECT delivery_date
FROM gf_orders
WHERE gf_order_history.id_order = gf_orders.id_order
AND gf_order_history.id_order_state = 4);
When I execute this code all the fields in id_order_state = 4 has been well updated but all the rest id_order_state which are different from 4 has been replaced to 0 .
Do you have any ideas what did I do wrong ?
All I want is the field id_order_state = 4 only to be updated.
Thank you very much for your help,
Sincerely,
Cyril
You forgot a where in your update query
UPDATE gf_order_history
SET date_add =
( SELECT delivery_date
FROM gf_orders
WHERE gf_order_history.id_order = gf_orders.id_order )
WHERE gf_order_history.id_order_state = 4

Not sure what is the execution stack of mySql fo my below query

So, I am trying to update the status_modified_time only if the status has changed, else keep it the same.
UPDATE table SET status = <new_status>,
status_modified_time = IF(status = <new_status>, status_modified_time, now()) WHERE id = <id>
this query makes status_modified_time = status_modified_time
UPDATE table SET status = <new_status>,
status_modified_time = IF(status = <old_status>, status_modified_time, now()) WHERE id = <id>
this query makes status_modified_time = now()
is it that mysql is updating the status field first and then checking the condition??
Left to right order of evaluation!
SQL UPDATE order of evaluation
I checked my query by updating the modified_time first.

Update Same mysql field twice in single query

I am not sure if its possible or not, Just want to know if it is. I have column plan_popular which has default value 0. Lets same i have a list :
Plan Name | plan_popular | amount
===================================
plan A 0 25.00
plan B 1 50.00
plan C 0 90.00
This is how i am doing:
$stmt = "update {CI}plans set plan_popular = 0";
$this->db->query($stmt);
$stmt2 = "update {CI}plans set plan_popular = 1 where plan_id = ?";
$this->db->query( $stmt2, array($plan_id) );
Now i have set the plan C to make. Now i want to reset it and want to make popular plan C to 1. What i am doing is running two queries, One i reset and make the plan_popular 0 and the second is get the update the plan C to 1 with there id. Is it possible in single query?
You can use an expression to determine the value to assign:
UPDATE {CI}plans
SET plan_popular = IF(plan_id = ?, 1, 0);
try this,
UPDATE {CI}plans
SET `plan_popular` = CASE `Plan Name`
WHEN 'plan C' THEN 1
ELSE 0
END
WHERE `Plan Name` IN((select `Plan Name` from {CI}plans where plan_popular=1 ) , 'plan C');
Updates can be expensive, what with manipulating locks, triggers and constraints firing, etc. In general, you want to avoid updating a field to the same value it already has. In English, if plan_id = variable and plan_popular is 0 then set it to 1 but if plan_id is any other value and plan_popular is 1 then set it to 0.
UPDATE {CI}Plans
SET plan_popular = if( plan_id = ?, 1, 0 )
where (plan_id = ? and plan_popular = 0)
or (plan_id <> ? and plan_popular = 1);
The where clause lets through only those rows that will actually be changed by the update. If this is a largish table, that can make quite a difference in response time. Logic is much less expensive than any actual operation that can performed in the database.

Why is it entering the loop? vb net mysql

I m new to vb net
So here is my problem
My database has 1 table=quiz and 3 columns of ID ,name and Flag
All flags are set to '0'
Whenever I delete ,instead of deleting ,I update the name field to xxx and Flag to 1
In my database ,ID =3 has name = xxx
Still y is it entering the loop? and printing the msg"OH no"?
my code :
Dim SQL as String
SQL = "SELECT Name FROM quiz WHERE Flag='0' AND ID='3'"
c = 0
If SQL <> "xxx"Then
' Try, Catch, Finally
i = i + 1
MsgBox("Ohh no")
END IF

Strange Sql Update behavior

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