Select the first and last line from group_concat when grouping sortable days

Referring to my previous questions about the concat mysql group again, group and display the rest of the rows

I need to get the first and last day from this request

eg

row 3 from 8,9,10 to first collumn 8, last collumn 10 row 5 from 21,22,23,24,28,29,30 to first collumn 21, last collumn 30 row 6 from 17,21,22,23,24,25 to first collumn 17 last collumn 25 SUBSTR(GROUP_CONCAT(DAY),-1) as fl 

BUT this gives me the last char, and there are several lines with 1 or 2 characters, for example

 1,2,3,22 1,3,6,3 

In the first example, this gives me 2, not 22: /

+6
source share
2 answers

Do not waste time analyzing the first and last from GROUP_CONCAT() . Instead, just insert MIN() and MAX() along with CONCAT() .

 SELECT user, CONCAT(MIN(DAY)), ',', MAX(DAY)) AS f1 FROM yourtable GROUP BY user 
+2
source

Another option (besides Michael's solution) is to use SUBSTRING_INDEX:

 SUBSTRING_INDEX(str,delim,count) 

from

 count = 1 

you get the first day

from

 count=-1 

you will get the last

+6
source

Source: https://habr.com/ru/post/916668/


All Articles