Reputation: 6119
Should I do single more complex MySQL query or multiple simpler queries with PHP?
For example
Simpler queries:
UPDATE image SET profile = 0 WHERE user_id = 1;
UPDATE image SET profile = 1 WHERE user_id = 1 AND id = 10;
One single more complex query:
UPDATE image
SET profile = CASE id WHEN 10 THEN 1 ELSE 0 END
WHERE user_id = 1;
1: What is fastest and most efficient?
2: What is considered to be best practice, or preferred method?
Upvotes: 6
Views: 244
Reputation: 108
One single query is fastest and most efficient
Besides the IF statement, MySQL also provides an alternative conditional statement called MySQL CASE. The MySQL CASE statement makes the code more readable and efficient. See more details... http://www.mysqltutorial.org/mysql-case-statement/
Upvotes: 1