mysql中的union all和case when示例

一、union即將多個表的數據合併到一個表中,union all保留重複行 示例,獲取用戶性別統計:code

select count(*) as total,sex from ((select userid,sex from `user_base_info_1` ) 
UNION ALL (select userid,sex from `user_base_info_2` )
UNION ALL (select userid,sex from `user_base_info_3` )
UNION ALL (select userid,sex from `user_base_info_4` )
UNION ALL (select userid,sex from `user_base_info_5` )
UNION ALL (select userid,sex from `user_base_info_6` )
UNION ALL (select userid,sex from `user_base_info_7` )
UNION ALL (select userid,sex from `user_base_info_8` )
UNION ALL (select userid,sex from `user_base_info_9` )
UNION ALL (select userid,sex from `user_base_info_10` )
UNION ALL (select userid,sex from `user_base_info_11` )
UNION ALL (select userid,sex from `user_base_info_12` )
UNION ALL (select userid,sex from `user_base_info_13` )
UNION ALL (select userid,sex from `user_base_info_14` )
UNION ALL (select userid,sex from `user_base_info_15` )
UNION ALL (select userid,sex from `user_base_info_16` )
UNION ALL (select userid,sex from `user_base_info_17` )
UNION ALL (select userid,sex from `user_base_info_18` )
UNION ALL (select userid,sex from `user_base_info_19` )
UNION ALL (select userid,sex from `user_base_info_20` )) as a group by sex

二、case where else對個別字段進行判斷處理 示例獲取不一樣年齡段的用戶統計:io

SELECT SUM(`人數`) AS total,`年齡段` FROM ( 
 (SELECT nnd AS '年齡段',COUNT(*) AS '人數' FROM(  
 SELECT   
 CASE  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<25 THEN '25如下'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=25 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=34 THEN '25-34'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=35 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=44 THEN '35-44'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=45 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=54 THEN '45-54' 
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=55 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=64 THEN '55-64'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=65 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=69 THEN '65-69'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=70 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=74 THEN '70-74'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=75 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=79 THEN '75-79'
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=80 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=84 THEN '80-84'
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=85  THEN '85以上'
  ELSE
  '未知'        
 END   
 AS nnd FROM user_base_info_1 
)a GROUP BY nnd) UNION ALL
(SELECT nnd AS '年齡段',COUNT(*) AS '人數' FROM(  
 SELECT   
 CASE  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<25 THEN '25如下'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=25 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=34 THEN '25-34'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=35 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=44 THEN '35-44'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=45 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=54 THEN '45-54' 
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=55 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=64 THEN '55-64'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=65 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=69 THEN '65-69'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=70 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=74 THEN '70-74'  
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=75 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=79 THEN '75-79'
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=80 AND (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<=84 THEN '80-84'
  WHEN (YEAR(NOW())-YEAR(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=85  THEN '85以上'
  ELSE
  '未知'        
 END   
 AS nnd FROM user_base_info_2 
)a GROUP BY nnd) 
) AS b GROUP BY `年齡段`;
相關文章
相關標籤/搜索