[英]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. 这是一个非常简单的版本,用于说明问题,本质上是translation_pivot用于进一步从一系列语言表中查找文本字符串,但这无关紧要。 Here, the title
, body
and link
columns in article
contain an id referencing content
from translation_pivot
. 在这里, title
, body
和link
列中article
包含ID引用content
从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
. 困难在于执行INNER JOIN将导致称为content
的列,该列仅包含translation_pivot
的第一个匹配项,在本例中为title
。
The other option I've looked into is using GROUP_CONCAT on translation_pivot.content
. 我研究过的另一个选项是在translation_pivot.content
上使用GROUP_CONCAT。 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). 这将起作用,但是然后我留下了用逗号分隔的项目列表,并且失去了与title
, body
和link
的明显关系,而不是第一,第二和第三项(还可以,但不是很好)。 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? group_concat_max_len的默认值为1024,可以更改,但是如果设置为高值,这是否会对性能产生影响?
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. 理想情况下,我想要一种方法,用translation_pivot
的文本结果替换title
, body
和link
列,或者至少将每个文本的内容作为单独的列返回。 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. 我的另一种选择是从translation_pivot中检索一个键值对作为数组,以id为键,然后在查询文章后进行查找。 This is only an extra query and probably a lot simpler. 这只是一个额外的查询,可能要简单得多。
Which solution will scale best? 哪种解决方案最合适? Or is there something else I'm missing? 还是我还缺少其他东西?
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
Link to the translation_pivot
table for each of title, body and link - like so: 链接到每个标题,正文和链接的translation_pivot
表-如下所示:
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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.