我想在如下表中显示有关 session 中最后 2 次注册的一些信息。例如,此表显示最后一次注册是由 John 进行的,是一次有 2 名参与者的注册,注册的总值(value)为 10.00。
User that did the registration | Quantity of participants registered | Total Value
John 2 10.00$
Jake 1 5.00$
您知道如何使用 sql 查询正确实现此目的吗?我像这样测试但它不起作用:http://www.sqlfiddle.com/#!9/225d82 .
select users.name,
count(participants.registration_id),
sum(registration_types.price)
from
users,
participants,
registration_types
Group by(users.name)
查询正在返回:
name count(participants.registration_id) sum(registration_types.price
Jake 8 80
John 8 80
但它应该返回:
name count(participants.registration_id) sum(registration_types.price
Jake 3 30
John 1 10
与问题相关的表格结构:
我有一个参与者表,用于存储 session 的所有参与者:
id registration_id registration_type_id name
1 1 1 Jake
2 1 1 Jane
3 1 2 Paul
4 2 2 Ben
注册表存储所有注册:
id status conference_id user_that_did_registration
1 incomplete 1 1 (User with id 1 did the registration in the conference with id 1)
2 incomplete 1 2
注册类型表存储注册类型:
id name price conference_id
1 rtype1 5.00 1
2 rype2 10.00 1
用户表
id name
1 John
2 Jake
最佳答案
从你的预期结果来看,你似乎需要这样做。
SELECT u.name,COUNT(pp.registration_id),SUM(rt.price)
FROM registration_types rt
INNER JOIN participants pp on pp.registration_id = rt.id
INNER JOIN users u on u.id = pp.registration_id
group by u.name
sqlfiddle:http://www.sqlfiddle.com/#!9/d28adf/8
[结果]:
| name | COUNT(pp.registration_id) | SUM(rt.price) |
|------|---------------------------|---------------|
| Jake | 3 | 30 |
| John | 1 | 10 |
注意:
- ANSI 92 标准
join
,因此请使用JOIN
而不是逗号,
,其中where
条件是旧样式的join
。
关于mysql - 获取 session 的最新注册信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014307/