Reputation: 4728
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
Upvotes: 0
Views: 57
Reputation: 11
Try this:
SELECT name
FROM people
WHERE age='20'
INTO OUTFILE '/tmp/name.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
Upvotes: -1
Reputation: 2813
select GROUP_CONCAT(name) AS name from people where age='20'
Upvotes: 2