baseten
baseten

Reputation: 1432

GROUP_CONCAT or alternative for grouping many to one MySQL results

I currently have database setup like so:

CREATE TABLE `article` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` int(11) NOT NULL,
  `body` int(11) NOT NULL,
  `link` int(11) NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE `translation_pivot` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` text,
  PRIMARY KEY (`id`)
)

This is quite a simplified version to illustrate the question, essentially the translation_pivot is used to further look up text strings from a series of language tables, but's that's not relevant. Here, the title, body and link columns in article contain an id referencing content from translation_pivot.

The difficulty is that doing an INNER JOIN will result in a column called content, which will contain only the first match from translation_pivot, in this case title.

The other option I've looked into is using GROUP_CONCAT on translation_pivot.content. This will work but then I'm left with a comma separated list of items and lose the obvious relation to title, body and link other than as the first, second and third items (which is ok, but not great). The more serious problem is that items in the translation can be several paragraphs of text. The default value for group_concat_max_len is 1024, which I can change but does this have performance implications if set to high values?

Ideally I'd like a way of replacing the title, body and link columns with the textual result from translation_pivot, or at least getting the textual content back for each as a separate column. Is this possible in a single query?

My other alternative is to retrieve key, value pairs as an array from translation_pivot with the id as the key and then do a lookup after querying the articles. This is only an extra query and probably a lot simpler.

Which solution will scale best? Or is there something else I'm missing?

Upvotes: 1

Views: 1638

Answers (2)

Eugen Rieck
Eugen Rieck

Reputation: 65314

Just do multiple joins:

SELECT
  article.id AS id,
  tptitle.content AS title,
  tpbody.content AS body,
  tplink.content AS link,
  article.`date` AS `date`
FROM
  article
  INNER jOIN translation_pivot AS tptitle ON article.title=tptitle.id
  INNER jOIN translation_pivot AS tpbody ON article.body=tpbody.id
  INNER jOIN translation_pivot AS tplink ON article.link=tplink.id

or:

SELECT
  article.id AS id,
  IFNULL(tptitle.content,'DEFAULT TITLE') AS title,
  IFNULL(tpbody.content, 'DEFAULT BODY') AS body,
  IFNULL(tplink.content, 'DEFAULT LINK') AS link,
  article.`date` AS `date`
FROM
  article
  LEFT jOIN translation_pivot AS tptitle ON article.title=tptitle.id
  LEFT jOIN translation_pivot AS tpbody ON article.body=tpbody.id
  LEFT jOIN translation_pivot AS tplink ON article.link=tplink.id

Upvotes: 2

user359040
user359040

Reputation:

Link to the translation_pivot table for each of title, body and link - like so:

select a.`id`,
       a.`date`,
       t.`content` title_content,
       b.`content` body_content,
       l.`content` link_content
from `article` a
left join `translation_pivot` t on a.`title` = t.`id`
left join `translation_pivot` b on a.`body` = b.`id`
left join `translation_pivot` l on a.`link` = l.`id`

Upvotes: 2

Related Questions