0

我在我的表中进行了标准化:

从 :tb_1

===================================
| id | doc_name | title  | author |
===================================
| 1  | doc1     | title1 | author1|
|    |          |        | author2|
| 2  | doc2     | title2 | author3|
|    |          |        | author4|
===================================

变得 :

tb_a                            tb_b
=======================       =========================
|id_a|doc_name| title |       |id_b| doc_name| author |
=======================       =========================
| 1  | doc1   | title1|       |  1 | title1  |author1 |
| 2  | doc2   | title2|       |  2 | title1  |author2 |
=======================       |  3 | title2  |author3 |
                              |  4 | title2  |author2 |
                              =========================

我想显示如下结果:

doc1 title1 author1 author2

doc2 title2 author3 author4

这是代码:

$query = mysql_query(" SELECT tb_a.doc_name, tb_a.title,
                              tb_b.doc_name, tb_b.author
                       FROM tb_a
                       JOIN  tb_b ON tb_a.doc_name = tb_b.doc_name ");
while ($row = mysql_fetch_array($query)) {
   $doc     = $row['doc_name'];
   $title   = $row['title'];
   $author  = $row['author'];

   echo $doc; echo $title ; echo $author;
}

但结果是: doc1 title1 author1 doc1 title1 author2 doc2 title2 author3 doc2 title2 author4

请帮助我,非常感谢你:)

4

2 回答 2

0

所以你正在尝试旋转你的桌子。不知道你想要你的输出,但你可以使用group_concat()因为你使用的是 mysql。

看起来像

   SELECT tb1.doc, tb1.title, group_concat(tb2.author)
     FROM tb1
     JOIN tb2 ON tb1.id = tb2.id
 GROUP BY tb1.doc, tb1.title
于 2012-09-29T23:37:05.930 回答
0
SELECT tb_a.doc_name, tb_a.title, GROUP_CONCAT(tb_b.author) authors
FROM tb_a
JOIN tb_b ON tb_a.title = tb_b.doc_name
GROUP BY tb_a.doc_name, tb_a.title
于 2012-09-30T00:51:09.390 回答