Link to home
Create AccountLog in
Avatar of Lightwalker
Lightwalker

asked on

MySQL query results as comma separated

Dear Experts,

I have a frustrating problem that I thought would be simple but I am not finding the solution.

I have a Query below:

SELECT orderdetails.DetailsID  FROM orderdetails WHERE orderdetails.ProgramActivated = 2

this would give me results such as:

DetailsID:
513
516

As you can see the results are in a list, where as what I want to do is have the results separated by a comma (,) to be used in another SQL query.

So the above results should be:

DetailsID
513, 516

I have been reading about GROUP_CONCAT which I was hoping would do it but I am not able to get it to work for me as the result is simply [BLOB] from the query below:

SELECT GROUP_CONCAT(orderdetails.DetailsID) AS DetailsID FROM orderdetails WHERE orderdetails.ProgramActivated = 2

Any suggestions?

Many thanks as always for your help.
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

You could maybe use a sub query like this, a bit depending on how your other sql query would look like:
select field1, field2 from other_table where DetailsID in
(
  SELECT orderdetails.DetailsID
  FROM orderdetails
  WHERE orderdetails.ProgramActivated = 2
);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of stilliard
stilliard
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Lightwalker
Lightwalker

ASKER

Dear stilliard,

you are a star. No I was not using phpmyadmin but I am using navicat for mysql as I tested the query.

As you mention about phpmyadmin I realized that I had not tested it inside my PHP script. When I did it shows up perfectly as comma seperated. So it is just in the navicat query editor it is showing up as blob.

Thank you for your insight.