Someone
Someone

Reputation: 736

Which is faster, a MySQL `CASE` statement or a PHP `if` statement?

Which makes website more speedy retrieve data from a MySQL Database or test value inside PHP script

It is interesting for me to find answer for this question. I have 2 options for displaying data in my website.

  1. To call from database like that:

    SELECT *, CASE WHEN u.sex='1' THEN 'Male' WHEN u.sex='2' THEN 'Female' END AS 'Sex' FROM users u LIMIT 30
    
  2. test value inside PHP script

    <?php
        if ($sex==1) {$sex='Male'} else {$sex='Female'}
    ?>
    

Upvotes: 3

Views: 1173

Answers (1)

Question Overflow
Question Overflow

Reputation: 11275

The first one is faster. There is no need to pull data to process it again at PHP layer.

Upvotes: 2

Related Questions