mysql - 获取 session 的最新注册信息

标签 mysql sql

我想在如下表中显示有关 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/

相关文章:

mysql - 多个大型 MySQL SELECT 查询——最好并行运行还是在队列中运行?

mysql - SQL 查询的 WHERE 子句中的 CASE 语句

php - MySQL 中的 If 语句?

mysql - 在不同条件下选择列两次

mysql - 汇总多个选择并加入

PHP MySQL表,可重用

mysql - 删除 MySQL 中的重复行_但_将所有引用记录重新分配给保留记录

php - php/mysql 环境中的 "forgot user password"标准类或脚本?

mysql连接条件-根据自定义顺序将连接限制为组中的单行

SQL Server : Select and count