如何从此存储过程中的查询返回行计数?我一直在尝试实现在其他类似的堆栈溢出帖子上找到的解决方案,但每个输出结果都是具有每个单独记录的计数的列。 (即,每列输出1,1,1,1,1 ......)
CREATE PROCEDURE stored_proc ()
BEGIN
SET @start_date = LAST_DAY(NOW() - INTERVAL 2 MONTH) + INTERVAL 1 DAY;
SET @end_date = LAST_DAY(NOW() - INTERVAL 1 MONTH);
SELECT users.*, count(DISTINCT mr_rev.id) AS prev_reg
FROM users
INNER JOIN gr_user_group
ON gr_user_group.user_id = wp_users.ID
AND gr_user_group.group_id != 22
AND gr_user_group.group_id = 5
INNER JOIN usermeta ON users.ID = usermeta.user_id
INNER JOIN mr_rev
ON users.ID = mr_rev.users_id AND mr_rev.mr_rev_types_id = 2
INNER JOIN mr_rev mrr
ON users.ID = mrr.users_id
AND mrr.mr_rev_types_id = 2
AND mrr.created_on >= @start_date
AND mrr.created_on <= @end_date
INNER JOIN mr_revs_meta
ON mr_rev.id = mr_revs_meta.mr_rev_id
AND mr_revs_meta.meta_value > @end_date
AND mr_revs_meta.created_at < @end_date
WHERE users.ID NOT IN ('101', '102')
GROUP BY users.ID
HAVING prev_reg > 15;
END;
是否有一种有效的方法来返回总行数?
编辑:Return count in Stored Procedure其中一个帖子我未成功实施。
答案 0 :(得分:1)
如何将查询包装在另一个选择中:
SELECT COUNT(*) FROM
(SELECT users.*, count(DISTINCT mr_rev.id) AS prev_reg
FROM users
INNER JOIN gr_user_group
ON gr_user_group.user_id = wp_users.ID
AND gr_user_group.group_id != 22
AND gr_user_group.group_id = 5
INNER JOIN usermeta ON users.ID = usermeta.user_id
INNER JOIN mr_rev
ON users.ID = mr_rev.users_id AND mr_rev.mr_rev_types_id = 2
INNER JOIN mr_rev mrr
ON users.ID = mrr.users_id
AND mrr.mr_rev_types_id = 2
AND mrr.created_on >= @start_date
AND mrr.created_on <= @end_date
INNER JOIN mr_revs_meta
ON mr_rev.id = mr_revs_meta.mr_rev_id
AND mr_revs_meta.meta_value > @end_date
AND mr_revs_meta.created_at < @end_date
WHERE users.ID NOT IN ('101', '102')
GROUP BY users.ID
HAVING prev_reg > 15 ) a;