0

i have the following tables: where one project may have many categories and one category may have many projects...

**project**
proj_id pk
proj_name 
proj_descr
proj_what_we_did
proj_url
proj_descr_url

**project_per_categories**
project_proj_id pk
category_cat_id pk

**category**
cat_id pk
cat_name 

and the following select:

SELECT proj_name, proj_descr, 
proj_what_we_did, proj_url, 
proj_descr_url, cat_name,  foto_url
FROM   project p, project_per_categories pc,
category c, foto f
WHERE  p.proj_id = pc.project_proj_id AND
pc.category_cat_id = c.cat_id AND
p.proj_id = f.foto_id
ORDER BY p.proj_id DESC
LIMIT 6

that is bringing the following result in var_dump:

    array (size=2)
   0 => 
     array (size=7)
       'proj_name' => string 'Rockable Magazine' (length=17)
       'proj_descr' => string 'Id nihil consectetur facilis assumenda minimau'(length=329)
       'proj_what_wi_did' => string 'Web Design' (length=10)
       'proj_url' => string 'http://localhost/datacode/projeto' (length=33)
       'proj_descr_url' => string 'Rockable-Magazine' (length=17)
       'cat_name' => string 'web design' (length=10)
       'foto_url' => string 'localhost/datacode/portfolio/case_study.jpg' (length=43)
   1 => 
     array (size=7)
       'proj_name' => string 'Rockable Magazine' (length=17)
       'proj_descr' => string 'Id nihil consectetur facilis assumenda minimau'(length=329)
       'proj_what_we_did' => string 'Web Design' (length=10)
       'proj_url' => string 'http://localhost/datacode/projeto' (length=33)
       'proj_descr_url' => string 'Rockable-Magazine' (length=17)
       'cat_name' => string 'css' (length=3)
       'foto_url' => string 'localhost/datacode/portfolio/case_study.jpg' (length=43)

now i will ask, note that only the cat_name have diferent values, so how can i implode or aggregate this two or more values in the cat_name field separated by commas ?

4

1 回答 1

1

查看group_concat函数。像这样的东西应该适合你:

SELECT 
  proj_name, 
  proj_descr, 
  proj_what_we_did, 
  proj_url, 
  proj_descr_url, 
  group_concat(cat_name),  
  foto_url
FROM   project p, 
  project_per_categories pc,
  category c, 
  foto f
WHERE  p.proj_id = pc.project_proj_id 
  AND pc.category_cat_id = c.cat_id 
  AND p.proj_id = f.foto_id
GROUP BY p.proj_id
ORDER BY p.proj_id DESC
LIMIT 6
于 2012-12-04T12:54:08.777 回答