
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Which is Faster: MySQL CASE Statement or PHP IF Statement
The MySQL CASE statement is faster in comparison to PHP if statement. The PHP if statement takes too much time because it loads data and then process while CASE statement does not.
Let us first create a table and work around an example of MySQL CASE statement −
mysql> create table DemoTable (Value int); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(500); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(1000); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 100 | | 500 | | 1000 | +-------+ 3 rows in set (0.00 sec)
Following is the query for MySQL CASE statement −
mysql> select Value, case when Value > 500 THEN "It is greater than or equal to 1000" else "It is lower than 1000" end as comparison from DemoTable;
This will produce the following output −
+-------+-------------------------------------+ | Value | comparison | +-------+-------------------------------------+ | 100 | It is lower than 1000 | | 500 | It is lower than 1000 | | 1000 | It is greater than or equal to 1000 | +-------+-------------------------------------+ 3 rows in set (0.00 sec)
Advertisements