dqkx69935 2017-03-07 16:00 采纳率: 100%
浏览 385
已采纳

为什么我在MySQL中使用SUM()时会得到重复的值?

I have a database for a school, and I want to list all users that belong to a specific course, and show the total amount that each student have pay for that course.

This is the query that I'm trying to get sorted:

SELECT    
  usuarios.userID AS userid, 
  cursos.nombreCurso, cursos.cursoPrecio, 
  SUM(pagos.pagoMonto) AS total,
  cursosUsuarios.examenes, 
  GROUP_CONCAT(DISTINCT rolesUsuarios.nombreRol SEPARATOR ',') AS 'roless',
  GROUP_CONCAT(DISTINCT pagos.pagoMonto SEPARATOR ',') AS 'pagoss'

FROM usuarios LEFT JOIN pagos
ON usuarios.userID = pagos.userID

LEFT JOIN rolesUsuarios
ON pagos.userID = rolesUsuarios.userID

LEFT JOIN cursos
ON pagos.cursoID = cursos.cursoID

LEFT JOIN cursosUsuarios
ON usuarios.userID = cursosUsuarios.userID
AND pagos.cursoID = cursosUsuarios.cursoID

WHERE pagos.estadoPago = 1
AND cursos.cursoID = 84
GROUP BY usuarios.userID

These are my tables:

usuarios (users)

CREATE TABLE usuarios(
    userID int unsigned not null auto_increment primary key,
    userEmail char(50) null,
    userApellido char(50) null,
    userNombres char(20) null
    UNIQUE(userEmail)
)

cursos (courses)

CREATE TABLE cursos (
    cursoID int unsigned not null auto_increment primary key,
    cursoID_old int(6) null
    nombreCurso char(100) not null,
    cursoPrecio int(10) null
)

cursosUsuarios

CREATE TABLE cursosUsuarios (
    cursosUsuariosID int unsigned not null auto_increment primary key,
    userID int not null,
    cursoID int not null,
    examenes int not null
)

pagos (payments for courses)

CREATE TABLE pagos (
    pagoID int unsigned not null auto_increment primary key,
    userID int not null,
    cursoID int null,
    pagoMonto int null,
    estadoPago int null
)

rolesUsuarios (user Roles)

CREATE TABLE rolesUsuarios (
    rolesUsuariosID int unsigned not null auto_increment primary key,
    userID int not null,
    nombreRol char(50) not null
)

This is the output that I'm getting:

 userID -- nombreCurso -- cursoPrecio -- total -- examenes -- roless -- pagoss 

 1 ------- Curso Demo --- 600 ---------- 300 ------ NULL ---- 1,2 ----- 100,50 

 2 ------- Curso Demo --- 600 ---------- 1000 ------ NULL --- 2,3 ----- 500

As you can see, the values shown in the field SUM(pagos.pagoMonto) AS total are multiplied by two. The last column shows the individual payments.

So total for user 1 should be 150 and for user 2 should be 500.

  • 写回答

1条回答 默认 最新

  • douyueju2912 2017-03-07 16:22
    关注

    I think that you miss a distinct in your SUM(), maybe try with SUM(DISTINCT pagos.pagoMonto)
    I hope this will help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)