views:

232

answers:

2
Is there any way to get the if statement to evaluate a query?

SELECT if(5>0,'EQ_Type','*') FROM EQUIPMENT;

Resulting in:
+-----------------------+
| IF(5>0,'EQ_Type','*') |
+-----------------------+
| EQ_Type               |
| EQ_Type               |
| EQ_Type               |
| EQ_Type               |
| EQ_Type               |
| EQ_Type               |
| EQ_Type               |
| EQ_Type               |
| EQ_Type               |
+-----------------------+
9 rows in set (0.00 sec)




I would like the above statement to be equivalent to the following:

SELECT 'EQ_Type' FROM EQUIPMENT;

And produce:
+--------------+
| EQ_Type      |
+--------------+
| ENGINE       |
| ENGINE       |
| ENGINE       |
| TRAILER      |
| TRAILER      |
| TRAILER      |
| WATER TENDER |
| WATER TENDER |
| WATER TENDER |
+--------------+


Thanks for any help

+3  A: 

Just remove the single quotes.

SELECT if(5>0,EQ_Type,'*') FROM EQUIPMENT;
Tor Valamo
Thank you sir. I am a novice so I figured it would be something obvious.
Dennis_M
+1  A: 

I recommend using ANSI notation - CASE statements in this case - rather than MySQL specific syntax:

SELECT CASE WHEN 5 > 0 THEN t.eq_type ELSE '*' END
  FROM EQUIPMENT t

Reason being, if you changed databases - this statement wouldn't need to be changed to work on Oracle, Postgres, SQL Server, etc.

OMG Ponies