在表中列出结果 - 同时在TD中循环和列表结果

时间:2014-04-02 19:12:26

标签: php mysql

我试图找到答案,但没有快乐。我有一个while循环,它给了我在表格中输出的大量行。它在这里:

$result = mysqli_query($con,"SELECT a.winner AS horseWinner, a.twitter_pubstatus,
a.market, a.racetime, a.racecourse, a.date_dd, a.date_mm, b.username, b.course, 
b.horse,b.type, b.racetime, b.dd, b.mm FROM results a 
INNER JOIN bets b ON a.winner = b.horse WHERE a.twitter_pubstatus = 0 
AND a.market = '$win' AND b.type = '$userwin' 
AND a.date_dd = b.dd AND a.date_mm = b.mm GROUP BY a.winner");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['horseWinner'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "</tr>";
}
echo "</table>";

这一切都很好,但问题在于每个horseWinner有多个用户名。我试图在while循环中循环执行另一个循环。所以我希望它在逗号分隔的第二列中列出用户名。

非常感谢

1 个答案:

答案 0 :(得分:2)

username上添加GROUP_CONCAT(),以便它返回以逗号分隔的列表 -

SELECT a.winner AS horseWinner, a.twitter_pubstatus,
a.market, a.racetime, a.racecourse, a.date_dd, a.date_mm, 

GROUP_CONCAT(b.username) as username, 

b.course, b.horse,b.type, b.racetime, b.dd, b.mm FROM results a 
INNER JOIN bets b ON a.winner = b.horse WHERE a.twitter_pubstatus = 0 
AND a.market = '$win' AND b.type = '$userwin' 
AND a.date_dd = b.dd AND a.date_mm = b.mm GROUP BY a.winner