我有像
这样的数据库column A Column B
2015-11-25 session 1
2015-11-26 session 1
2015-11-27 session 1
2015-11-28 session 2
我希望显示
Next session 1 is on - 25 Nov 2015, 26 Nov 2015, 27 Nov 2015
我尝试了GROUP BY列B,但它只返回一个第一个值。即2015年11月25日。
<?php
$sql = mysql_query("select * from table GROUP BY column B HAVING COUNT(*) > 1 order by column A ASC");
while($data=mysql_fetch_array($sql)){
?>
Next <?=$data['column B']?> Is On <b><?=date("d F Y",strtotime($data['coulmn A']));?>
<?}?>
答案 0 :(得分:2)
使用GROUP_CONCAT
,concat
或group by
Select
concat('Next ',b,' is on - ',GROUP_CONCAT(DATE_FORMAT(a,'%d %b %Y')))
From table group by b
或DATE_FORMAT
用于更改日期格式
答案 1 :(得分:2)
试一试:
<?php
$sql = mysql_query("select ColumnA, ColumnB from table GROUP BY columnB, ColumnA HAVING COUNT(*) > 1 order by column A ASC");
while($data=mysql_fetch_array($sql)){
?>
Next <?=$data['columnB']?> Is On <b><?=date("d F Y",strtotime($data['coulmnA']));?>
<?}?>
答案 2 :(得分:2)
这可能不是一种简单的方法,而是工作..
$result1 = mysqli_query($con, "select columnB from table GROUP BY columnB, columnB HAVING COUNT(*) > 1 ");
while($data1 = mysqli_fetch_assoc($result1)){
$ses = $data1['columnB'];
$result2 = mysqli_query($con, "select columnA, columnB from table where columnB='$ses'");
while($data2 = mysqli_fetch_assoc($result2)){
var_dump($data2);
}
}