user2341693
user2341693

Reputation: 132

Mysql Query order by

I have a very complex query. I dont want to start from scratch. So I am pasting here a result set data which need to be formatted based on some constraints.

My query is as follows..

SELECT
  GROUP_CONCAT(fix_seq SEPARATOR '-') AS fix_seq,
  apt,
  star,
  trans,
  fix
FROM
  tabe
GROUP BY
  star, trans
ORDER BY
  seq;

Problem here is I need to get the sequence of fixes by grouping the star and trans and taking the seq in ascending order.But in the result if we observe we can see that there is a problem in the order of concatenation.

But the order is not preserving.Can anyone spot where am i going wrong?Thanks in advance.

Upvotes: 3

Views: 98

Answers (2)

Enrique Gil
Enrique Gil

Reputation: 754

Use ORDER BY statement,

GROUP_CONCAT(fix_seq ORDER BY seq SEPARATOR '-')
AS fix_seq,apt,star,trans,fix 
from tabe
group by star,trans
order by seq;

Upvotes: 0

John Woo
John Woo

Reputation: 263693

add ORDER BY clause inside the function,

GROUP_CONCAT(fix_seq ORDER BY seq SEPARATOR '-')

Basic syntax:

GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])

Upvotes: 4

Related Questions