美文网首页
分组统计查询和存储过程

分组统计查询和存储过程

作者: 翔子161919 | 来源:发表于2020-07-10 18:37 被阅读0次

一、分组查询并统计各分组数量

SELECT *,COUNT(*) as be_liked_num FROM

(SELECT c.*,v.uid as author_id

FROM ypt_collect_object c

LEFT JOIN ypt_short_video v ON c.collect_id = v.id

WHERE c.type = 2 and c.collect_type = 5

ORDER BY c.id desc) as collect_info

GROUP BY collect_info.collect_id;

二、合并查询结果

(SELECT *,COUNT(*) as be_liked_num FROM

(SELECT c.*,v.uid as author_id

FROM ypt_collect_object c

LEFT JOIN ypt_short_video v ON c.collect_id = v.id

WHERE c.type = 2 and c.collect_type = 5

ORDER BY c.id desc) as collect_info

GROUP BY collect_info.collect_id)

UNION

(SELECT *,COUNT(*) as be_liked_num FROM

(SELECT c.*,v.user_id as author_id

FROM ypt_collect_object c

LEFT JOIN ypt_comments v ON c.collect_id = v.id

WHERE c.type = 2 and c.collect_type = 8

ORDER BY c.id desc) as collect_info

GROUP BY collect_info.collect_id)

ORDER BY addtime desc)

三、存储过程

BEGIN

#Routine body goes here...

# 定义变量

  DECLARE begin_num INT(11);

SET begin_num = page * page_size;

SELECT * FROM (

(SELECT *,COUNT(*) as be_liked_num FROM

(SELECT c.*,v.uid as author_id

FROM ypt_collect_object c

LEFT JOIN ypt_short_video v ON c.collect_id = v.id

WHERE c.type = 2 and c.collect_type = 5

ORDER BY c.id desc) as collect_info

GROUP BY collect_info.collect_id)

UNION

(SELECT *,COUNT(*) as be_liked_num FROM

(SELECT c.*,v.user_id as author_id

FROM ypt_collect_object c

LEFT JOIN ypt_comments v ON c.collect_id = v.id

WHERE c.type = 2 and c.collect_type = 8

ORDER BY c.id desc) as collect_info

GROUP BY collect_info.collect_id)

ORDER BY addtime desc

) as info WHERE info.author_id = user_id

LIMIT begin_num,page_size;

END

相关文章

  • 分组统计查询和存储过程

    一、分组查询并统计各分组数量 SELECT *,COUNT(*) as be_liked_num FROM (SE...

  • need

    sql语句执行原理,顺序 存储过程使用场景 分组 联结查询 索引

  • 分组统计查询

    一、统计函数count(*| [distinct] 字段)、max(字段)、min(字段)、sum(数字字段)、a...

  • 2018-09-05 mysql、redis

    1、多表联合查询 查询没有被购买过的商品 查询哪类商品是销量冠军 分组只能写分组字段和统计字段,写其它字段报错进入...

  • Mysql常用功能

    MYSQL常用及存储过程一、常用查询语句1)LIKE的灵活运用 2)分组查询 实例:查询骑手商城的商品中定价大于1...

  • SQL查询语句1

    复杂查询 = 简单查询 + 限定查询 + 查询排序 + 多表查询 + 分组统计查询 + 子查询。 面对所有的复杂查...

  • MySQL插入一亿条数据

    创建数据表 创建用户表 创建存储过程 调用存储过程插入一亿条数据 查看表空间 插入数据测试统计 查询测试

  • mysql语句

    分组查询: 分组查询统计并且降序排序 2.更改cash_user表中的reg_channel列值为'kaniu_m...

  • 温故知新-MYSQL-一些查询技巧(二)

    通过出生年月查询平均年龄 case when 语法查询分类信息 根据时间(日期)分组查询 存储的为时分秒,要按照天分组

  • Mysql 存储过程查询总价,函数查询总价

    函数过程查询总价 存储过程查询总价 函数

网友评论

      本文标题:分组统计查询和存储过程

      本文链接:https://www.haomeiwen.com/subject/baamcktx.html