Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created April 18, 2014 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betweenbrain/11068237 to your computer and use it in GitHub Desktop.
Save betweenbrain/11068237 to your computer and use it in GitHub Desktop.
MySQL Joomla GROUP_CONCAT Examples
// SQL
SELECT
`users`.`id`,
`users`.`name`,
(SELECT
GROUP_CONCAT(`k2`.`title`)
FROM
`jos_k2_items` AS `k2`
WHERE
`k2`.`created_by` = `users`.`id`
) as categories
FROM
`jos_users` AS `users`;
// Joomla
$query = ' SELECT ' .
$this->db->nameQuote('users.id') . ', ' .
$this->db->nameQuote('users.name') . ', ' .
' (SELECT ' .
' GROUP_CONCAT(' . $this->db->nameQuote('k2.title') . ')' .
' FROM' .
$this->db->nameQuote('jos_k2_items') . ' AS ' . $this->db->nameQuote('k2') .
' WHERE ' .
$this->db->nameQuote('k2.created_by') . ' = ' . $this->db->nameQuote('users.id') .
') AS ' . $this->db->nameQuote('categories') .
' FROM ' .
$this->db->nameQuote('jos_users') . ' AS ' . $this->db->nameQuote('users');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment