繁体   English   中英

PHP MySQL查询选择DISTINCT,但按日期显示Limited Rows顺序

[英]PHP MySQL query select DISTINCT but display Limited Rows order by date

我需要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 

以下是查询,

$query = mysql_query('select DISTINCT `column name` from table1 ORDER BY col3 DESC LIMIT 2');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM