I'm using MySQL, and I want to do a sort of ternary statement in my SQL like:
SELECT USER_ID, ((USER_ID = 1) ? 1 : 0) AS FIRST_USER FROM USER
The results would be similar to:
USER_ID | FIRST_USER 1 | 1 2 | 0 3 | 0 etc.
How does one accomplish this?
The MySQL IF() function is used for validating a condition. The IF() function returns a value if the condition is TRUE and another value if the condition is FALSE. The MySQL IF() function can return values that can be either numeric or strings depending upon the context in which the function is used.
Introduction to MySQL SELECT statement First, specify one or more columns from which you want to select data after the SELECT keyword. If the select_list has multiple columns, you need to separate them by a comma ( , ). Second, specify the name of the table from which you want to select data after the FROM keyword.
SELECT USER_ID, (CASE USER_ID WHEN 1 THEN 1 ELSE 0 END) as FIRST_USER FROM USER
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