Mohammad.Amin
Mohammad.Amin

Reputation: 84

how to display multiple rows in one row with same id in mysql?

i have a view in mysql and i want to show two rows with the same id in one row. Please see following pic :

enter image description here

please help me : i want to show expr1 column in one column

Upvotes: 2

Views: 8110

Answers (2)

Raging Bull
Raging Bull

Reputation: 18747

Use GROUP_CONCAT:

SELECT POST_ID,TITLE,USERNAME,DATE_TIME,COMMENT_COUNT,GROUP_CONCAT(Expr1) as Expr1
FROM TableName
GROUP BY POST_ID,TITLE,USERNAME,DATE_TIME,COMMENT_COUNT

Result will be:

POST_ID  TITLE         USERNAME   DATE_TIME                 COMMENT_COUNT   Expr1
1        HELLO WORLD   AMIN       2014-01-01 00:00:00.000   0               OS,windows xp

Upvotes: 5

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

As it seems data in all other columns are matching, grouping by id may work.
But it is always suggested to group by those columns that are chosen in select

select 
  post_id, title, 
  username, date_time, 
  comment_count, 
  group_concat( expr1 separator ' ' ) Expr1
from table_name
group by 1, 2, 3, 4, 5 -- positions of columns in the select

Upvotes: 0

Related Questions