这是sql表
id id_user id_userfor description date
1 231 119 a to b 2010-02-05 17:42:47
2 231 231 myself 2010-02-05 17:36:28
3 231 119 from x to b 2010-02-05 17:36:29
我找不到选择输出结果的方法,如下所示:
user 119
a to b 2010-02-05 17:42:47
from x to b 2010-02-05 17:36:29
user 231
myself 2010-02-05 17:36:28
答案 0 :(得分:2)
您可以使用
select id_userfor, description, date from table order by id_userfor;
然后您可以打印每个不同date
id_userfor
列数据
$old = '';
while($row = mysql_fetch_array($result)) {
if($old != $row['id_userfor']) {
$old = $row['id_userfor'];
echo "$row['id_userfor']<br />";
}
echo $row['description'] . " " . $row['date'] . "<br />";
}
答案 1 :(得分:1)
您可以使用id_userfor
字段进行查询排序然后循环所有记录,并且每次遇到新值id_userfor
时都会打印它。
这样的事情:
$SQL = 'select * from ... order by `id_userfor`';
$Result = ...;
$PrevUserFor = '';
while($Row = ...) {
const $UserFor = $Row['id_userfor'];
if ($PrevUserFor != $UserFor) {
// Here is where you print the user ID
echo "user $UserFor<br>";
$PrevUserFor != $UserFor;
}
$Description = $Row['description'];
$Date = $Row['date'];
echo(' $Description $Date<br>');
}
希望这有帮助。
答案 2 :(得分:1)
codaddict查询更具可读性:
SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`
然后,在循环内部,你会使用这样的东西:
$sql = "SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`";
$query = mysql_query($sql);
$user = null; // Empty user
while($data = mysql_fetch_assoc($query)) {
if ($data['id_userfor'] != $user) {
// Display the user only when it changes
echo '<H1>'. $data['id_userfor'] .'</H1>';
$user = $data['id_userfor'];
}
// Display the rest of the fields here :)
}