views:

72

answers:

1

Hi folks,

I am trying to export some data from two tables bridged by a third table that stores the one (file) to many (keywords) relationship.

Tables are like so:

files
id, url, title

keywords
id, word

bridge
file, word

What I want is to produce an export that has one row per file like this:

files.id, files.url, files.title, keyword1|keyword2|keyword3|...

Any advice greatly appreciated!

JG

+2  A: 

You can use GROUP_CONCAT to combine the keywords in a GROUP BY query:

SELECT 
  files.id, files.url, files.title,
  GROUP_CONCAT(keywords.word ORDER BY keywords.word SEPARATOR '|') keywords
FROM
  files
  LEFT OUTER JOIN bridge ON bridge.file = files.id
  LEFT OUTER JOIN keywords ON keywords.id = bridge.word
GROUP BY
  files.id, files.url, files.title
Phil Ross
Thank you very much for the reply Phil! That solved it and gave me some new things to study/play with! I really need to get an education in SQL more than just learning it as problems arise. I taught myself perl and PHP but SQL is coming more slowly to me. Any recommendations for learning it in a comprehensive and organized way?
jerrygarciuh