Note that there are some explanatory texts on larger screens.

plurals
  1. POConcatenate and Count the multiple rows in mysql
    primarykey
    data
    text
    <p>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<br></p> <p><strong>the query to create the table:</strong></p> <pre> 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; </pre> <p>Now, below the query I'm using to concatenate the data.</p> <pre> SELECT fk_number_lesson AS lesson, GROUP_CONCAT( licao_study ORDER BY licao_study ) AS study FROM tb_presence GROUP BY fk_number_lesson; </pre> <p>the result: <em>*see that the query could concatenate the data:</em></p> <pre> <b>lesson study</b> 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 </pre> <p>Now, I want to "count" the same and keep that data concatenation. My wish is that adding the "count" the result looks like this:</p> <pre> 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)<br> </pre> <p><em>*column obs is only demo that's not what I want result</em></p> <p>is it possible to make a query like this?<br> <strong><em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>_</strong><br></p> <p><em>today</em><br> 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)</p> <p>but when the query results lack some places no response, ex. "lesson 2" is only the result (0,1), concatenation would be (1,1).</p> <p>I like it when the result of the Select looks like this:</p> <pre> <b>lesson study obs </b> 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 </pre> <p>the result in the query you made looks like this:</p> <pre> lesson study obs(Count) 1 1,2,1 (1=0) (2=0.5+0.5) (2=1+1)<br> 2 2,3 (2=0.5+0.5) (3=1+1+1) 3 2,3 (2=0+0) (3=1+1+1)<br> </pre> <p>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</p> <pre> lesson study obs(Count) 1 1,2,1 (1=0) (2=0.5+0.5) (2=1+1)<br> 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)<br> </pre> <p>is that possible?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload