假设我有以下模式,为了便于您参考,我使用的是MySQL:
纸张(paperId,标题)
作者(authorId,authorName)
论文作者(paperId,authorId)
我这样设计表格是因为一篇论文可能有多个作者,而一个作者可能写多篇论文。
我现在想要一篇论文的标题和所有作者。
我一直在想我能不能在一份声明中做到这一点?
如果是肯定的,那怎么办?到目前为止,我只是想从MySQL中获得一组作者。
如果它是一个NO,我如何做才能保持事务的原子性?(让我们考虑将标题和作者作为一个事务处理)
编辑-针对Justin E的评论:
我想买这样的东西:

array(
    "title" => "ABC"
    "authors" => array("Ken", "Kitty", "Keith")
)

最佳答案

MySQL GROUP_CONCAT()是你所需要的(也许PHP explode()
SQL Fiddle
MySQL 5.5模式设置:

CREATE TABLE Paper (`paperId` int, `title` varchar(7));
INSERT INTO Paper (`paperId`, `title`)
VALUES (1, 'hello'),(2, 'hola'),(3, 'bonjour');

CREATE TABLE Author (`authorId` int, `authorName` varchar(3));
INSERT INTO Author (`authorId`, `authorName`)
VALUES (1, 'me'),(2, 'moi');

CREATE TABLE Paper_Author (`paperId` int, `authorId` int);
INSERT INTO Paper_Author (`paperId`, `authorId`)
VALUES (1, 1),(1, 2),(2, 2);

问题1:
SELECT Paper.`title`, GROUP_CONCAT(Author.`authorName`) as `authors`
FROM Paper_Author
INNER JOIN Paper
  ON Paper.`paperId` = Paper_Author.`paperId`
INNER JOIN Author
  ON Author.`authorId` = Paper_Author.`authorId`
GROUP BY Paper.`title`

Results
| title | authors |
|-------|---------|
| hello |  me,moi |
|  hola |     moi |

08-17 08:18