mysql左连查询弊端_mysql – 使用多个左连接查询 – 点列值不正确

我有以下数据库结构,我正在尝试运行一个查询,该查询将显示教室,有多少学生是教室的一部分,教室分配了多少奖励,以及分配给单个教授的积分数量教室(基于classroom_id专栏).

在最底层使用查询我正在尝试收集教室分配的’totalPoints’ – 基于在classroom_redeemed_codes表中计算points列并将其作为单个整数返回.

由于某种原因,totalPoints的值不正确 – 我做错了但不确定是什么……

我的结构:

CREATE TABLE `organisation_classrooms` (

`classroom_id` int(11) NOT NULL AUTO_INCREMENT,`title` varchar(255) NOT NULL,`active` tinyint(1) NOT NULL,`organisation_id` int(11) NOT NULL,`period` int(1) DEFAULT '0',`classroom_bg` int(2) DEFAULT '3',`sortby` varchar(6) NOT NULL DEFAULT 'points',`sound` int(1) DEFAULT '0',PRIMARY KEY (`classroom_id`)

);

CREATE TABLE organisation_classrooms_myusers (

`classroom_id` int(11) NOT NULL,`user_id` bigint(11) unsigned NOT NULL,);

CREATE TABLE `classroom_redeemed_codes` (

`redeemed_code_id` int(11) NOT NULL AUTO_INCREMENT,`myuser_id` bigint(11) unsigned NOT NULL DEFAULT '0',`ssuser_id` bigint(11) NOT NULL DEFAULT '0',`classroom_id` int(11) NOT NULL,`order_product_id` int(11) NOT NULL DEFAULT '0',`order_product_images_id` int(11) NOT NULL DEFAULT '0',`date_redeemed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,`points` int(11) NOT NULL,`type` int(1) NOT NULL DEFAULT '0',`notified` int(1) NOT NULL DEFAULT '0',`inactive` tinyint(3) NOT NULL,PRIMARY KEY (`redeemed_code_id`),);

SELECT

t.classroom_id,title,COALESCE (

COUNT(DISTINCT r.redeemed_code_id),0

) AS totalRewards,COALESCE (

COUNT(DISTINCT ocm.user_id),0

) AS totalStudents,COALESCE (sum(r.points),0) AS totalPoints

FROM

`organisation_classrooms` `t`

LEFT OUTER JOIN classroom_redeemed_codes r ON (

r.classroom_id = t.classroom_id

AND r.inactive = 0

AND (

r.date_redeemed >= 1393286400

OR r.date_redeemed = 0

)

)

LEFT OUTER JOIN organisation_classrooms_myusers ocm ON (

ocm.classroom_id = t.classroom_id

)

WHERE

t.organisation_id =37383

GROUP BY title

ORDER BY t.classroom_id ASC

LIMIT 10

– 编辑 –

OOPS!我有时讨厌SQL …我犯了一个大错误,我试图计算在classroom_redeemed_codes而不是organisation_classrooms_myuser表中的学生人数.我真的很抱歉我应该早点把它拿走?!

classroom_id | totalUniqueStudents

16 1

17 2

46 1

51 1

52 1

在classroom_redeemed_codes表中有7行,但由于teacher_id 46有两行,尽管具有相同的myuser_id(这是学生ID),这应该显示为一个唯一的学生.

这有意义吗?基本上试图根据myuser_id列获取classroom_redeemed_codes表中唯一学生的数量.

例如,一个教室id 46可以在classroom_redeemed_codes表中有100行,但是如果它们相同的myuser_id则应该显示totalUniqueStudents计为1而不是100.

如果不清楚,请告诉我……

– 更新 –

我有以下查询似乎工作借用了一个似乎工作的用户…(我的头痛)我会再次接受答案.很抱歉这个混乱 – 我想我只是在想这个

select crc.classroom_id,COUNT(DISTINCT crc.myuser_id) AS users,COUNT( DISTINCT crc.redeemed_code_id ) AS classRewards,SUM( crc.points ) as classPoints,t.title

from classroom_redeemed_codes crc

JOIN organisation_classrooms t

ON crc.classroom_id = t.classroom_id

AND t.organisation_id = 37383

where crc.inactive = 0

AND ( crc.date_redeemed >= 1393286400

OR crc.date_redeemed = 0 )

group by crc.classroom_id

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值