For a one to many relationship like news
to news_files
, in order to get them all into one query you will, as you have found, need to retrieve duplicate column values from the "one" for each related row in the "many" table.
然而,我质疑是否需要在单一问题上这样做。 只有一个到一个关系,如果只有一个<条码>,就会有: 《<>新闻>/代码>浏览,试图在一栏中生产这些字眼,是有意义的。 但是,对于一位到许多人来说,你或许也只是问一下主要表格,然后问一下“many”关系表的相关段落。
/* Returns duplicate column values for each news_files row */
/* and all columns from both tables */
SELECT
n.*,
nf.*
FROM
news n
LEFT OUTER JOIN news_files nf ON n.Id = nf.newsid
WHERE n.Id = 1
您可检索one栏,news_files
,作为使用GROUP_CONCAT()
的压缩名单,但该数字与仅仅检索所有浏览量完全相同。 虽然MySQL允许在<代码>中不出现一栏。 按<<<>代码/代码>列出的组别,如/code>,则其他RDBMS t,即为可携带性,GROUP_CONCAT()
被置于一个子类中,以便与主表合并并检索所有栏目。
/* Comma-separated list of filePath and all cols from news */
SELECT
n.*,
nf.files
FROM
news
LEFT OUTER JOIN (
SELECT newsid, GROUP_CONCAT(filePath) AS files FROM news_feeds
GROUP BY newsid
) nf ON n.Id = nf.newsid