Php 连接并计算mysql中的多行

Php 连接并计算mysql中的多行,php,mysql,sql,count,concatenation,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 NU

我想连接并计算同一列的数据,因此我可以连接,但不能计算重复的数据。见下表

创建表的查询:

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; 删除表(如果存在)`tb_presence`; 创建表“tb_状态”( `代码_presence`smallint(6)无符号非空自动增量, `李考大学研究“varchar(10)非空, `fk_编号_课程'varchar(4)不为空, 主键(`code\u presence`) )ENGINE=InnoDB AUTO_INCREMENT=35默认字符集=utf8; 开始; 在“tb_状态”值('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','1','1','3'),('11','1','1','1','1','1',('12','0','2','2'),('13','1','3'),'16','0','1','1','1','18','2','0','5',',('19', '1', '2'), ('20', '0.5', '3'); 犯罪 设置外键检查=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; 选择fk_编号_课程作为课程, 作为研究的科考特小组(科考特研究所的科考特研究顺序) 来自tb_的存在 按fk_编号_课程分组; 结果:*查看查询是否可以连接数据:

课业研究 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 现在,我想“计数”相同,并保持数据连接。 我的愿望是添加“计数”结果如下所示:

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)
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)
课程学习obs(计数) 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) 32,2,3(2=0+0)(2=0.5+0.5)(3=1+1+1)
*列obs是唯一的演示,这不是我想要的结果

可以这样进行查询吗?
\Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

今天
在您帮助解决的上述案例中,我得到了所有预期结果,例如列“lesson 1”至少有一次结果(0,05,1),那么连接和计数将是(1,1,1)

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

我喜欢选择的结果如下所示:

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)
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)
课程学习obs 1 0,0.5,0.5,1,1这条线是完整的,有3个结果 2 0.5,0.5,1,1,1该行没有所有结果,只有0.5和1 3 0,0,1,1,1这条线没有所有结果,只有0.5和1 您所做查询的结果如下所示:

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)
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)
课程学习obs(计数) 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) 32,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)
课程学习obs(计数) 1,2,1(1=0)(2=0.5+0.5)(2=1+1)
20,2,3(加上“0”零)(2=0.5+0.5)(3=1+1+1) 3 2,3(2=0+0)(加上“0”零)(3=1+1+1)

有可能吗?

只计算列中重复的单词

按列名称从表名称组中选择列名称、计数(*)

编辑:

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;

嗨,Madhivan,我想进一步改进这个查询。你能帮我吗?如果一个类没有要查询的记录,可以添加0吗?可以添加查询吗?你可以发布预期的结果吗?请等一分钟,我正在写结果。I Madhivan,我把这个结果放在预期的位置上…非常感谢