0

If I have a table that lists names and ages then can I run a single query that will give me all names of people with a particular age.

e.g.

name      age
----------|-------
alice      20
bob        21
chris      20
dave       23
eric       26
fred       29
greg       20

I want my query to return a list, separated by a comma, of all people who are aged 20.

e.g.

select (concat(name,',')) from people where age='20'

And this outputs:

alice,chris,greg

Obviously I could just do:

select name from people where age='20'

And then loop through the results in PHP but I am trying to do all of this in a query

0

3 Answers 3

2
select GROUP_CONCAT(name) AS name from people where age='20'
Sign up to request clarification or add additional context in comments.

Comments

1

try

select GROUP_CONCAT(name) from people where age='20'

Comments

-1

Try this:

SELECT name
FROM people
WHERE age='20'
INTO OUTFILE '/tmp/name.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

1 Comment

interesting approach - not sure if it work - but incomplete: how to get the file back into PHP? (anyways - I think the accepted answer is much easier)

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.