0

I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data. see below the table

the query to create the table:

    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;

Now, below the query I'm using to concatenate the data.

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

FROM tb_presence
GROUP BY fk_number_lesson;

the result: *see that the query could concatenate the data:

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

Now, I want to "count" the same and keep that data concatenation. My wish is that adding the "count" the result looks like this:

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 is only demo that's not what I want result

is it possible to make a query like this?
__________________________________________________

today
In the above case you helped solve, I have all the expected results, ex. column "lesson 1" has the results (0,05,1) at least once, then concatenation and count would be (1,1,1)

but when the query results lack some places no response, ex. "lesson 2" is only the result (0,1), concatenation would be (1,1).

I like it when the result of the Select looks like this:

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

the result in the query you made looks like this:

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)

but I wish it had not when some of these results (0, 0.5, 1) in the lessons, the query add "0" in place: see below

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)

is that possible?

4

2 に答える 2

0

列で繰り返される単語のみをカウントするには

column_name で table_name グループから column_name, count(*) を選択します

于 2013-11-06T13:09:18.910 に答える