我需要DISTINCT的帮助。我想显示Distinct行但是只按日期显示最新的拖车行(col3)
来自数据库的此表示例:
+----+-----+------------+
|col1|col2 |col3 |
+----+-----+------------+
|A |one |1-jul-2013 |
|A |two |2-jul-2013 |
|A |three|3-jul-2013 |
|A |four |4-jul-2013 |
|A |five |5-jul-2013 |
|B |one |1-jul-2013 |
|B |two |2-jul-2013 |
|B |three|3-jul-2013 |
|B |four |4-jul-2013 |
|B |five |5-jul-2013 |
|B |six |6-jul-2013 |
|B |seven|7-jul-2013 |
|B |eight|8-jul-2013 |
+----+-----+-----+
$query = mysql_query('select * from table1 ORDER BY col3 DESC');
$results = array();
while($row = mysql_fetch_assoc($query)) {
$col1= $row['col1'];
// This is basically grouping your rows by col1
if(!isset($results[$col1]))
$results[$col1] = array();
$results[$col1][] = $row;
}
echo "<tr>";
foreach($results as $col1=> $rows) {
echo "<td>".$col1."</td>";
foreach($rows as $row) {
echo "<td>".$row['col2']."</td>";
}
echo"</tr>";
}
我需要对它进行一些修改..我想要区别并且只显示最新的2行
我希望显示器看起来像这样:
A |one |two
B |one |two
答案 0 :(得分:0)
以下是查询
$query = mysql_query('select DISTINCT `column name` from table1 ORDER BY col3 DESC LIMIT 2');