Home >Database >Mysql Tutorial >Can from be followed by case in mysql?
Yes, MySQL allows the use of CASE expressions in the FROM clause to select specific rows. The syntax is: SELECT ... FROM table_name WHERE
AND CASE WHEN THEN THEN ELSE END AS , where
#MySQL FROM Can I use CASE expression later?
Answer: Yes
Detailed explanation:
MySQL allows use in the FROM clause CASE expression to select specific rows from a table. A CASE expression is essentially a conditional statement that can return different values based on given conditions.Syntax:
<code>SELECT ... FROM table_name WHERE <filter_condition> AND CASE WHEN <condition1> THEN <value> WHEN <condition2> THEN <value> ELSE <default_value> END AS <alias></code>Where:
is required before applying the CASE expression conditions met.
and
are the conditions to be evaluated.
is the value returned based on the condition.
is the default value returned if no condition is matched.
is the alias assigned to the result of the CASE expression.
Example:
Suppose there is a "customers" table with the following columns:
<code>SELECT * FROM customers WHERE country = 'United States' AND CASE WHEN age > 18 THEN 'Adult' WHEN age <= 18 THEN 'Minor' ELSE 'Unknown' END AS `age_group`;</code>This query will return all customers from the United States and classify them as adults, minors, or unknown based on age.
The above is the detailed content of Can from be followed by case in mysql?. For more information, please follow other related articles on the PHP Chinese website!