This request:
SELECT e.id entry_id
, GROUP_CONCAT(t.txt ORDER BY t.txt) tags
FROM entry e
LEFT JOIN entry_tag et
ON e.id = et.entry_id
LEFT JOIN tag t
ON et.tag_id = t.id
GROUP BY e.id
Will return tags as a comma separated list. You can read additional parameters on GROUP_CONCAT: http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_group-concat
. , php explode: http://php.net/manual/en/function.explode.php
tag entry_tag, GROUP_CONCAT - (, JSON) GROUP_CONCAT , , :
$sql = '
SELECT e.id entry_id
, t.id tag_id
, t.txt tag_text
, t.published tag_published
FROM entry e
LEFT JOIN entry_tag et
ON e.id = et.entry_id
LEFT JOIN tag t
ON et.tag_id = t.id
ORDER BY e.id
';
$result = mysql_query($ql);
$entry_id = NULL;
$entry_rows = NULL;
while ($row = mysql_fetch_assoc($result)) {
if ($entry_id != $row['entry_id']) {
if (isset($entry_id)) {
process_entry($entry_rows);
}
$entry_id = $row['entry_id'];
$entry_rows = array();
}
$entry_rows[] = $row;
}
if (isset($entry_id)){
process_entry($entry_rows);
}