计算mysql中每个订单的订单项总数 [英] Count total number of order item of each order in mysql

查看:846
本文介绍了计算mysql中每个订单的订单项总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下表


|order_item_id|order_id|customer_id|
|     2       |   30   |    9      | 
|     3       |   30   |    9      | 
|     4       |   30   |    9      | 
|     5       |   30   |    9      | 
|     11      |   32   |    9      | 
|     12      |   32   |    9      | 
|     13      |   32   |    9      | 

在这里,我想使用mysql计算每个order_id的order_item_id总数。
请帮助

here i would like to count total number of order_item_id for each order_id using mysql. please help

推荐答案

尝试一下:

select order_id, count(*) from t
group by order_id



来计数(*)

编辑:


是的,我知道,但实际上我想列出所有内容相关记录,而不仅仅是计数。 – user804457

yes this one i knew it, but actually i would like to list out all related records as well, not just count. – user804457

更改需求后,这似乎就是您要查找的内容:

After the requirements changed, then this seems to be what you're looking for:

select * from t t1
join (
  select order_id, count(*) aCount from t
  group by order_id
) t2
on t1.order_id = t2.order_id

结果:

+---------------+----------+-------------+--------+
| ORDER_ITEM_ID | ORDER_ID | CUSTOMER_ID | ACOUNT |
+---------------+----------+-------------+--------+
|             2 |       30 |           9 |      4 |
|             3 |       30 |           9 |      4 |
|             4 |       30 |           9 |      4 |
|             5 |       30 |           9 |      4 |
|            11 |       32 |           9 |      3 |
|            12 |       32 |           9 |      3 |
|            13 |       32 |           9 |      3 |
+---------------+----------+-------------+--------+

这篇关于计算mysql中每个订单的订单项总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆