我正在运行以下查询,并使用php显示结果:
<?php
$con=mysqli_connect("host","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select
p.id,
p.date,
(select
t.title
from
table02 t
where
p.family = t.family) title,
(select
a.author
from
table03 a
where
p.family = a.family) author,
(select
n.note
from
table04 n
where
p.family = n.family) note,
from
table01 p
where
p.family in (48766 , 276197, 265242, 334879)";
$result = mysqli_query($con,$query);
echo "<table border='1'><tr><th>ID</th><th>Date</th><th>Title</th><th>Author</th><th>Note</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['note'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>所以我们有一张这样的桌子:
ID -日期
01 - 07/01/2013 -谢谢你的帮助-一些家伙- 8.3
07 - 07/03/2013 -不客气-尼斯先生- 7.6
11 - 09/27/2013 -希望你喜欢我们- J. Growth - 8.9
等。
如果我只为每一列找到一个结果,这就很好了。
但问题是,我们可以有多个作者为一些“歌曲”,如我们将看到(当ID = 13)。
因此,当我们运行查询时,我们会收到一条消息,即table03中的author具有多个值,并且没有显示任何内容。
我怎么能有一个表给我这个专栏的结果与多个作者呢?
ID -日期
01 - 07/01/2013 -谢谢你的帮助-一些家伙- 8.3
07 - 07/03/2013 -不客气-尼斯先生- 7.6
11 - 09/27/2013 -希望你喜欢我们- J. Growth - 8.9
2013年11月14日-休斯顿,我们有问题了- B. Lee & T. Hanks - 6.4
17 - 12/09/2013 -现在我们只有一个- P. Neuer - 7.1
非常感谢!
发布于 2014-04-05 23:25:50
一种选择是在author子查询中使用MySQL的GROUP_CONCAT()。
对于您的情况,子查询可能如下所示:
(select
GROUP_CONCAT(a.author)
from
table03 a
where
p.family = a.family) authors注:
组中值之间的默认分隔符是逗号(“,”)
发布于 2014-04-05 23:32:32
在子查询中使用限制(可选顺序) clausule,在子查询中也使用GROUP_CONCAT()分组函数。
极限=> https://dev.mysql.com/doc/refman/5.5/en/select.html
GROUP_CONCAT => group-concat
https://stackoverflow.com/questions/22888135
复制相似问题