连接并计算mysql中的多行

时间:2013-11-06 12:57:05

标签: php mysql sql count concatenation

我想连接和计算同一列的数据,所以我可以连接但我不能计算重复的数据。见表格下方

创建表格的查询:


    DROP TABLE IF EXISTS `tb_presence`;
CREATE TABLE `tb_presence` (
  `code_presence` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `licao_study` varchar(10) NOT NULL,
  `fk_number_lesson` varchar(4) NOT NULL,
  PRIMARY KEY (`code_presence`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;
BEGIN;
INSERT INTO `tb_presence` VALUES ('1', '0.5', '2'), ('2', '0', '2'), ('3', '0.5', '1'), ('4', '1', '3'), ('5', '1', '2'), ('6', '0.5', '1'), ('7', '0.5', '3'), ('8', '0', '3'), ('9', '1', '1'), ('10', '1', '3'), ('11', '1', '1'), ('12', '0', '2'), ('13', '1', '3'), ('14', '0', '3'), ('16', '0', '1'), ('17', '1', '2'), ('18', '0.5', '2'), ('19', '1', '2'), ('20', '0.5', '3');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

现在,在我用来连接数据的查询下面。


    SELECT fk_number_lesson AS lesson,
GROUP_CONCAT( licao_study ORDER BY licao_study ) AS study

FROM tb_presence
GROUP BY fk_number_lesson;

结果: *看到查询可以连接数据:


lesson   study
1        0,0.5,0.5,1,1
2        0,0,0.5,0.5,1,1,1
3        0,0,0.5,0.5,1,1,1

现在,我想“统计”相同并保持数据连接。 我希望添加“count”结果如下:


lesson      study       obs(Count)
1           1,2,1       (1=0) (2=0.5+0.5) (2=1+1)
2           2,2,3       (2=0+0) (2=0.5+0.5) (3=1+1+1)
3           2,2,3       (2=0+0) (2=0.5+0.5) (3=1+1+1)

* column obs只是演示不是我想要的结果

是否可以像这样进行查询?
_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

今天
在上面你帮助解决的情况下,我有所有预期的结果,例如。列“第1课”的结果(0,05,1)至少有一次,然后连接和计数将是(1,1,1)

但是当查询结果缺少某些地方没有响应时,例如。 “第2课”只是结果(0,1),连接是(1,1)。

当Select的结果如下所示时,我喜欢它:

lesson   study              obs 
1        0,0.5,0.5,1,1       this line is complete there is the 3 results
2        0.5,0.5,1,1,1       this line does not have all the results, only 0.5 and 1
3        0,0,1,1,1           this line does not have all the results, only 0.5 and 1

您所做查询的结果如下所示:

lesson      study       obs(Count)
1           1,2,1       (1=0) (2=0.5+0.5) (2=1+1)
2 2,3 (2=0.5+0.5) (3=1+1+1) 3 2,3 (2=0+0) (3=1+1+1)

但我希望在课程中有些结果(0,0.5,1)时,查询中添加“0”:见下文


lesson      study       obs(Count)
1           1,2,1       (1=0) (2=0.5+0.5) (2=1+1)
2 0,2,3 (add "0" zero)(2=0.5+0.5) (3=1+1+1) 3 2,3 (2=0+0) (add "0" zero) (3=1+1+1)

可能吗?

2 个答案:

答案 0 :(得分:0)

仅计算列

中重复的单词
  

按column_name

从table_name group中选择column_name,count(*)

答案 1 :(得分:0)

select lesson,GROUP_CONCAT(study) as study from
(
    SELECT fk_number_lesson AS lesson,
count( licao_study ) AS study
FROM tb_presence
GROUP BY fk_number_lesson,licao_study
) as t group by lesson;

编辑:

select lesson,GROUP_CONCAT(study) as study from
(
    select t1.lesson,t2.licao_study,coalesce(study,0) as study from 
    (
        select distinct fk_number_lesson as lesson,n from tb_presence,
    (
        select 0 as n union all select 0.5 union all select 1.0
    ) as t
    ) as t1 left join 
    (
        SELECT fk_number_lesson AS lesson,licao_study,
    count( licao_study ) AS study
    FROM tb_presence
    GROUP BY fk_number_lesson,licao_study
    ) as t2
        on t1.n=t2.licao_study and t1.lesson=t2.lesson 
) as t group by lesson;