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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With