Mysql 多个子查询 多个LEFT JOIN 视图创建

来源:互联网 发布:网络段子精选哲理 编辑:程序博客网 时间:2024/05/16 06:07

CREATE VIEW `v_noprovide` AS SELECT*FROMtb_sectionWHEREprovide = '0'CREATE VIEW `v_thismonth` AS SELECTts.userId AS id,CONCAT('thismonth:',SUM(ts.amount),'yuan') AS 'thismonth'FROMtb_section AS tsWHEREts.yearMonth = DATE_FORMAT(NOW(), '%Y%m')CREATE VIEW `v_lastmonth` AS SELECTts.userId AS id,CONCAT('lastmonth:',SUM(ts.amount),'yuan') AS 'lastmonth'FROMtb_section AS tsWHEREts.yearMonth = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH),'%Y%m')CREATE VIEW `v_usermanage` ASSELECTu.id AS id,u.email AS email,u.`name` AS `name`,CONCAT(COALESCE (la.lastmonth,'lastmonth:0yuan'),COALESCE (th.thismonth,'thismonth:0yuan')) AS recent,Sum(COALESCE(b.amount,0)) AS totalBonus,Sum(COALESCE(n.amount,0)) AS unbilledFROMtb_user AS uLEFT JOIN tb_bonus AS b ON u.id = b.receiverIdLEFT JOIN v_noprovide AS n ON u.id = n.userIdLEFT JOIN v_thismonth AS th ON u.id = th.idLEFT JOIN v_lastmonth AS la ON u.id = la.idGROUP BY u.id

1 0