mysql 加入 3 table 但 "COUNT" 是重复的
mysql join 3 table but the "COUNT" be duplicate
我有 3 个这样的 table
订单table:
| id | product_id | status | created_at |
|----| ---------- | ------ | ---------- |
| 1 | 1 | done | 1607431726 |
| 2 | 7 | done | 1607431726 |
| 3 | 8 | done | 1607431726 |
产品table:
| id | user_id | title | description | created_at |
|----| ------- | ----------- | ------------- | ---------- |
| 1 | 1 | product 1 | description 1 | 1607431726 |
| 7 | 3 | product 2 | description 1 | 1607431726 |
| 8 | 3 | product 3 | description 1 | 1607431726 |
评分table:
| id | client_id | content_type | content_id | rating | created_at |
|----| --------- | ------------ | ---------- | ------ | ---------- |
| 1 | 5 | user | 1 | 5 | 1607431726 |
| 2 | 4 | user | 3 | 5 | 1607431726 |
| 3 | 5 | user | 3 | 4 | 1607431726 |
从上面的 3 tables 中,我想得到 1 个结果,其中有一个字段 average_rating/user,总计 order/user,我想按 average_rating 排序] & total_rating 描述。大致是这样的结果:
| user_id | average_rating | total_order |
| ------- | -------------- | ----------- |
| 1 | 5.0 | 1 |
| 3 | 4.5 | 2 |
这是我的查询:
SELECT b.user_id, round(avg(c.rating), 1) as total_rating, COUNT(a.id) as total_order
FROM orders a
LEFT JOIN products b ON a.product_id=b.id
LEFT JOIN ratings c ON c.content_id=b.user_id
WHERE a.status = 'done'
AND c.content_type = 'user'
GROUP BY b.user_id, c.content_id;
但是对于我的查询,user_id 1 的总订单 return 1 和 user_id 3 的总订单 4,结果是:
| user_id | average_rating | total_order |
| ------- | -------------- | ----------- |
| 1 | 5.0 | 1 |
| 3 | 4.5 | 4 |
我已经尝试过 INNER JOIN
、LEFT OUTER
、RIGHT OUTER
、RIGHT JOIN
,但结果是一样的。
谁能帮帮我?
每个产品有多个评级。一种选择使用 distinct
:
SELECT p.user_id,
round(avg(r.rating), 1) as total_rating,
COUNT(distinct o.id) as total_order
FROM orders o
INNER JOIN products p ON o.product_id=p.id
INNER JOIN ratings r ON r.content_id=p.user_id
WHERE o.status = 'done' AND r.content_type = 'user'
GROUP BY p.user_id, r.content_id;
我有 3 个这样的 table
订单table:
| id | product_id | status | created_at |
|----| ---------- | ------ | ---------- |
| 1 | 1 | done | 1607431726 |
| 2 | 7 | done | 1607431726 |
| 3 | 8 | done | 1607431726 |
产品table:
| id | user_id | title | description | created_at |
|----| ------- | ----------- | ------------- | ---------- |
| 1 | 1 | product 1 | description 1 | 1607431726 |
| 7 | 3 | product 2 | description 1 | 1607431726 |
| 8 | 3 | product 3 | description 1 | 1607431726 |
评分table:
| id | client_id | content_type | content_id | rating | created_at |
|----| --------- | ------------ | ---------- | ------ | ---------- |
| 1 | 5 | user | 1 | 5 | 1607431726 |
| 2 | 4 | user | 3 | 5 | 1607431726 |
| 3 | 5 | user | 3 | 4 | 1607431726 |
从上面的 3 tables 中,我想得到 1 个结果,其中有一个字段 average_rating/user,总计 order/user,我想按 average_rating 排序] & total_rating 描述。大致是这样的结果:
| user_id | average_rating | total_order |
| ------- | -------------- | ----------- |
| 1 | 5.0 | 1 |
| 3 | 4.5 | 2 |
这是我的查询:
SELECT b.user_id, round(avg(c.rating), 1) as total_rating, COUNT(a.id) as total_order
FROM orders a
LEFT JOIN products b ON a.product_id=b.id
LEFT JOIN ratings c ON c.content_id=b.user_id
WHERE a.status = 'done'
AND c.content_type = 'user'
GROUP BY b.user_id, c.content_id;
但是对于我的查询,user_id 1 的总订单 return 1 和 user_id 3 的总订单 4,结果是:
| user_id | average_rating | total_order |
| ------- | -------------- | ----------- |
| 1 | 5.0 | 1 |
| 3 | 4.5 | 4 |
我已经尝试过 INNER JOIN
、LEFT OUTER
、RIGHT OUTER
、RIGHT JOIN
,但结果是一样的。
谁能帮帮我?
每个产品有多个评级。一种选择使用 distinct
:
SELECT p.user_id,
round(avg(r.rating), 1) as total_rating,
COUNT(distinct o.id) as total_order
FROM orders o
INNER JOIN products p ON o.product_id=p.id
INNER JOIN ratings r ON r.content_id=p.user_id
WHERE o.status = 'done' AND r.content_type = 'user'
GROUP BY p.user_id, r.content_id;