1

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.

1 Answer 1

1
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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.