Imagine I have a table called preferences tracking users' e-mail preferences. It has the following columns:
preferences_id(primary key)user_id(link to theusertable, not unique for various reasons)marketing_emails(boolean)security_emails(boolean)handwritten_emails(boolean)
Now, by default, I consider all e-mail types to be enabled, however, for various reasons, I cannot create a row in the preferences table for every user right after they register, only after they change their preferences.
So when a user disables marketing e-mails, I want to either:
Insert a new row like:
INSERT INTO preferences (user_id, marketing_emails, security_emails, handwritten_emails) VALUES (?,0,1,1,);OR
Update an existing row for the user like:
UPDATE preferences SET marketing_emails = 0 WHERE user_id = ?;
Is there a way to combine both cases in a single query in MySQL, or do I have to first do a separate check if a row for the given user_id already exists and then execute one of the two queries?