网站地图    收藏   

主页 > 后端 > mysql数据库 >

Mysql多个子查询多个LEFTJOIN视图创建 - mysql数据库

来源:自学PHP网    时间:2015-04-16 10:51 作者: 阅读:

[导读] CREATE VIEW `v_noprovide` AS SELECT*FROMtb_sectionWHEREprovide = 0 CREATE VIEW `v_thismonth` AS SELECTts userId AS id,CONCAT( thismonth: ,SUM(ts amount), yuan )...

CREATE VIEW `v_noprovide` AS SELECT
	*
FROM
	tb_section
WHERE
	provide = '0'




CREATE VIEW `v_thismonth` AS SELECT
	ts.userId AS id,
	CONCAT(
		'thismonth:',
		SUM(ts.amount),
		'yuan'
	) AS 'thismonth'
FROM
	tb_section AS ts
WHERE
	ts.yearMonth = DATE_FORMAT(NOW(), '%Y%m')






CREATE VIEW `v_lastmonth` AS SELECT
	ts.userId AS id,
	CONCAT(
		'lastmonth:',
		SUM(ts.amount),
		'yuan'
	) AS 'lastmonth'
FROM
	tb_section AS ts
WHERE
	ts.yearMonth = DATE_FORMAT(
		DATE_SUB(NOW(), INTERVAL 1 MONTH),
		'%Y%m'
	)







CREATE 
VIEW `v_usermanage` AS
SELECT
	u.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 unbilled
FROM
	tb_user AS u
LEFT JOIN tb_bonus AS b ON u.id = b.receiverId
LEFT JOIN v_noprovide AS n ON u.id = n.userId
LEFT JOIN v_thismonth AS th ON u.id = th.id
LEFT JOIN v_lastmonth AS la ON u.id = la.id
GROUP BY u.id

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论