<?
require("config.php");
$result = mysql_query("select s_title, p_name, date from tbl_slist_info where publish='u' ORDER By date ") or die(mysql_error());
while($row=mysql_fetch_assoc($result))
{ ?>
<tr>
<td colspan="3"><?=$row['date']?></td>
</tr>
<tr>
<td><a href="g.php?title=<?=$row['s_title'] ?>"><?=$row['s_title'] ?></a></td>
<td><a href="p.php?title=<?=$row['p_name'] ?>"><?=$row['p_name'] ?></a></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我想要一个mysql查询,以便我可以根据日期显示记录.假设在1月1日我有5个请求.1月2日我有4个请求.通过这种方式
1-Jan
_____
A type1
B type1
C type2
D type1
E type3
2-Jan
____
W type4
X type1
Y type3
Z type5
Run Code Online (Sandbox Code Playgroud)
可以在数据库级别执行此操作:
SELECT concat(date, concat('\n__________\n',
group_concat(concat(concat(name, '\t'), type) SEPARATOR '\n')))
FROM table_name
GROUP BY date;
Run Code Online (Sandbox Code Playgroud)
您可能应该在应用程序级别执行此操作:迭代结果集时,检查当前日期是否与上一行的日期不同以检测组更改.
所以像这样:
$lastDate = null;
while($row = mysql_fetch_assoc($result)) {
if ($row['date'] != $lastDate) {
echo '<tr><td colspan="2">'.htmlspecialchars($row['date']).'</td></tr>';
$lastDate = $row['date'];
}
echo '<tr>';
echo '<td><a href="g.php?title='.urlencode($row['s_title']).'">'
.htmlspecialchars($row['s_title']) .'</a></td>';
echo '<td><a href="p.php?title='.urlencode($row['p_name']).'">'
.htmlspecialchars($row['p_name']).'</a></td>';
echo '</tr>';
}
Run Code Online (Sandbox Code Playgroud)