MySQL常用SQL

一、case when 统计

SELECT 
    COUNT(*) user_count,
    COUNT(CASE
        WHEN sex = '0' THEN sex
    END) AS un_known_count,
    COUNT(CASE
        WHEN sex = '1' THEN sex
    END) AS male_count,
    COUNT(CASE
        WHEN sex = '2' THEN sex
    END) AS female_count
FROM
    user;

你可能感兴趣的:(MySQL常用SQL)