我需要DISTINCT的幫助。我想呈現出不同的行,但日期只顯示最新拖行順序(COL3)PHP MySQL查詢選擇DISTINCT,但顯示有限行按日期排序
實例此表從數據庫:
+----+-----+------------+
|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
將「LIMIT 2」添加到查詢結尾處? – DragonYen 2014-10-16 16:58:17
花一些時間來正確地格式化您的問題。 – Ejaz 2014-10-16 17:00:52
這是MySQL。我看不到任何日期,只有字符串和整數 – Strawberry 2014-10-16 19:02:19