Reputation: 6217
Does MySQL make it possible to do something like:
SELECT username, age, age>13 AS ageGT FROM users
.
And get something like:
+--------+---+-----+
|username|age|ageGT|
+--------+---+-----+
|fred |14 |true |
|bob |12 |false|
+--------+---+-----+
?
This would be a huge help, thanks!
Clarification: I'm not looking for a WHERE
clause, I want to select both cases, but have a column showing whether the clause evaluates true or false.
Upvotes: 4
Views: 4232
Reputation: 23125
You can do CASE WHEN age > 13 THEN 'true' ELSE 'false' END AS ageGT
:
SELECT
username,
age,
CASE WHEN age > 13 THEN 'true' ELSE 'false' END AS ageGT
FROM users
Or more simply:
IF(age>13, 'true', 'false') AS ageGT
Read more on CASE Expressions and the IF() Function.
An important difference is that the CASE
expression syntax is supported by all major DBMSs, whereas IF()
is pretty much MySQL specific.
Upvotes: 9
Reputation: 36631
SELECT username,
age,
case when age>13 then 'true' else 'false' end AS ageGT
FROM users
Upvotes: 2