When case when encounters an empty string to 0, case empty string
Note: If the field is of the varchar type, quotation marks are required for the condition after when.
SELECT (CASE 'marate_status' WHEN 0 then' married 'when 1 then' unmarried 'else' weizhi' END) AS marital_status FROM tj_archive WHERE id = 'd1407280006'
In the preceding statement, if marital_status is a null string instead of null, the query result is "married"
Finally, the correct result is:
SELECT (CASE 'marital _ status' WHEN '0' then' married 'when' 1 'then' unassociated 'else' weizhi' END) AS marital_status FROM tj_archive WHERE id = 'd1407280006'
The red part is a different sentence.
Why does the SQL statement use CASE WHEN to convert columns into NULL values?
This is because statistics are made by men and women separately.
Select -- avg (total score), avg (case when gender = 'male' then total score else 0 END) AS 'average score of boys ', avg (case when gender = 'femal' then total else 0 END) AS 'average female score 'from students -- group by gender -- remove this group by try
How can I put case when in where?
It cannot be placed in the where clause. For example, it is easier to use a subquery to query the select * from where field in (select case when field> 1 then 1 else 0 end from table)