我在表格中进行了规范化:
来自: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
请帮帮我,非常感谢你:)。
答案 0 :(得分: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
答案 1 :(得分: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