3

I'm having problems using MySQL's IF or CASE syntax

The procedure goes something like this:

I have a PHP project that calls a MySQL query, let's say PHP passes variable X.

If X is not 0 then it should perform a query something like:

SELECT * FROM table
WHERE id = X

But if X is 0. then it should perform a query to just select all records:

SELECT * FROM table

I don't want to have the condition in PHP which calls a different MySQL query based on the result of the condition in PHP. I want the if / else condition to be performed inside the SQL query based on the variable passed X.

2 Answers 2

3
SELECT * FROM table
WHERE $x = 0 or id = $x
7
  • 1
    down voter, care to comment? this is one of the nicest queries speed wise I have seen posted.
    – AdrianBR
    Oct 16, 2013 at 10:06
  • That wasn't my downvote, but I think that was because there's no X column (but there's $x from PHP)
    – Alma Do
    Oct 16, 2013 at 10:10
  • I thought that was obvious :)
    – kgu87
    Oct 16, 2013 at 10:12
  • @Alma Didn't say it was 'fastest', but certainly more extendable for additional conditions :)
    – kgu87
    Oct 16, 2013 at 10:33
  • @Alma however, $x = 0 or id = $x is indeed faster than equivalent if()
    – kgu87
    Oct 16, 2013 at 12:02
1

I assume $x is derived from your PHP, then:

SELECT * FROM t WHERE IF($x=0, 1, id=$x)

(it's to show general idea, you'll have to handle injections e t.c. by yourself)

Edit

I've found nice code in another answer here. So I was curious - which is faster? Now I'm 'happy' that mine is that as well:

My version:

mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.5.27    |
+-----------+
1 row in set (0.00 sec)

Now, test data:

mysql> select * from test;
+----+-------+
| id | title |
+----+-------+
|  1 | f     |
|  3 | t     |
|  4 | s     |
+----+-------+
3 rows in set (0.02 sec)

And tests:

First: IF comparison

mysql> select @x;
+------+
| @x   |
+------+
| t    |
+------+
1 row in set (0.00 sec)

mysql> select benchmark(1E7, if(@x=0, 1, title=@x)) from test;
+---------------------------------------+
| benchmark(1E7, if(@x=0, 1, title=@x)) |
+---------------------------------------+
|                                     0 |
|                                     0 |
|                                     0 |
+---------------------------------------+
3 rows in set (1.66 sec)

mysql> set @x=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select benchmark(1E7, if(@x=0, 1, title=@x)) from test;
+---------------------------------------+
| benchmark(1E7, if(@x=0, 1, title=@x)) |
+---------------------------------------+
|                                     0 |
|                                     0 |
|                                     0 |
+---------------------------------------+
3 rows in set (1.85 sec)

Second, OR comparison

mysql> select @x;
+------+
| @x   |
+------+
|    0 |
+------+
1 row in set (0.00 sec)

mysql> select benchmark(1E7, title = @X or @X = 0) from test;
+--------------------------------------+
| benchmark(1E7, title = @X or @X = 0) |
+--------------------------------------+
|                                    0 |
|                                    0 |
|                                    0 |
+--------------------------------------+
3 rows in set, 65535 warnings (17.31 sec)

-ok, that was because of type-casting. Fixing:

mysql> set @x='0';
Query OK, 0 rows affected (0.00 sec)

mysql> select benchmark(1E7, title = @X or @X = '0') from test;
+----------------------------------------+
| benchmark(1E7, title = @X or @X = '0') |
+----------------------------------------+
|                                      0 |
|                                      0 |
|                                      0 |
+----------------------------------------+
3 rows in set (5.78 sec)

And, finally, non-zero:

mysql> set @x='t';
Query OK, 0 rows affected (0.00 sec)

mysql> select benchmark(1E7, title = @X or @X = '0') from test;
+----------------------------------------+
| benchmark(1E7, title = @X or @X = '0') |
+----------------------------------------+
|                                      0 |
|                                      0 |
|                                      0 |
+----------------------------------------+
3 rows in set (4.92 sec)

Conclusion

IF comparison seems to be much more faster in this case, than OR (about 3 times for 1E7 benchmark iterations)

2
  • can you explain what the 1 means? yes, there are no problems with the injections, just SQL statement
    – galao
    Oct 16, 2013 at 10:08
  • 1 means that it will be WHERE 1, i.e. always true for any row - i.e. entire table because any row will match
    – Alma Do
    Oct 16, 2013 at 10:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.