因此,我试图每天对不同的行进行排序,并仅输出不同唯一行的一些“ testdate”信息,但是我遇到的问题是,只有第一个id才能获得正确的输出“ testdate”,而不是其他所有ID。请看一下我现在所拥有和正在寻找的示例。
这是我的桌子,名称为“ 1702”:
╔════╦══════════╦═════════════════════╗
║ id ║ testdate ║ datecreated ║
╠════╬══════════╬═════════════════════╣
║ 1 ║ 1702 ║ 2019-02-16 14:48:28 ║
║ 2 ║ 1702 ║ 2019-02-17 14:48:58 ║
║ 8 ║ 1802 ║ 2019-02-16 14:50:07 ║
║ 4 ║ 1702 ║ 2019-02-17 14:48:51 ║
║ 7 ║ 1802 ║ 2019-02-17 14:50:34 ║
║ 6 ║ 1702 ║ 2019-02-17 14:48:54 ║
║ 9 ║ 1802 ║ 2019-02-16 14:50:09 ║
║ 10 ║ 1802 ║ 2019-02-17 14:50:12 ║
║ 11 ║ 1602 ║ 2019-02-14 14:50:55 ║
║ 12 ║ 1602 ║ 2019-02-14 14:51:11 ║
╚════╩══════════╩═════════════════════╝
我当前的结果是这样
2019-02-17
2,4,7,6,10 1702
2019-02-16
1,8,9 1702
2019-02-14
11,12 1602
但是我希望它像这样:
2019-02-17
2 1702
4 1702
7 1802
6 1702
10 1802
2019-02-16
1 1702
8 1802
9 1802
2019-02-14
11 1602
12 1602
这是我的代码:
$sql = 'SELECT DATE(datecreated), GROUP_CONCAT(id) as grouped_name, testdate FROM `1702` GROUP BY DATE(datecreated) DESC';
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['DATE(datecreated)'].'<br>';
echo '
<div class="box-bettype">
<p class="box-bettype-text">'. $row['grouped_name']. ' ' . $row ['testdate'] . '</p>
</div>
';
}
答案 0 :(得分:1)
尝试一下:
$sql = 'SELECT DATE(datecreated) as datecreated, id as grouped_name, testdate FROM `1702` ORDER BY DATE(datecreated) DESC';
$result = mysqli_query($conn,$sql);
$prevDate = null;
while ($row = mysqli_fetch_assoc($result)) {
echo $row['datecreated'] !== $prevDate ? $row['datecreated'].'<br>' : '';
$prevDate = $row['datecreated'];
echo '
<div class="box-bettype">
<p class="box-bettype-text">'. $row['grouped_name']. ' ' . $row ['testdate'] . '</p>
</div>
';
}
我删除了GROUP_CONCAT,只是将当前日期与以前的日期进行了比较。