2

I have a SQL query where I am getting value of column in list form.

Select STUFF((
              Select Distinct ',' + QUOTENAME(visit_count) 
              From arms 
              Where armId= '@armIdValue'
              FOR XML PATH(''), TYPE
             ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

the value of visit_count column is 5, so I get result like this:

[1],[2],[3],[4],[5]

I tried to turn this query to run in MySQL but it's not accepting FOR XML.
Do any solutions ?

2 Answers 2

1

You need to use group_concat()

select group_concat(concat('[',visit_count,']')
from arms WHERE armId= 12 group by armid
Sign up to request clarification or add additional context in comments.

6 Comments

it doesn't give result in form of list, it simply returns 5
could you please add some sample data in your question @MohammadShahbaz
I have a table where 1 column is armId and other is visit_count, data is armId=12 and visit_count=5
@MohammadShahbaz, I've updated the answer - you can check but sample data is more appreciated
got 1,2,3,4,5 as result. only thing which i am not getting is the [ ]
|
1

You can try the Following Query.

MSSQL:

DECLARE @col nvarchar(MAX);
SELECT @col = COALESCE(@col,'') +'['+visit_count +']'+ ',' 
From arms Where armId= '@armIdValue'
SELECT @col

MYSQL :

SELECT armId,GROUP_CONCAT(visit_count)
 FROM arms Where armId= '@armIdValue'
 GROUP BY armId

4 Comments

getting Error: DECLARE col nvarchar(MAX) Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE col nvarchar(MAX)' at line 1 0.000 sec
col is @col in the above statement
how can i run these threes statements together in mysql, it's giving me error
FUNCTION QUOTENAME does not exist 0.000 sec

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.