选择数据库上每个表的最后一条记录

时间:2014-03-25 04:54:55

标签: php mysql sql

我想从数据库中显示每个的最后一条记录。所有表都具有与以下相同的结构:

+------+-----------+------------+--------+-----------+
|  id  |   kh_ho   |   kh_ha    | total  | base_num  |
+------+-----------+------------+--------+-----------+
| 1    |   2400    |     2000   | 2569   | 12548     |
| 2    |   2600    |     2195   | 2569   | 12548     |
| 3    |   2800    |     2400   | 2569   | 12548     |
| 4    |   4700    |     4450   | 2569   | 12548     |
| 5    |   5200    |     5000   | 2569   | 12548     |
| 6    |   4800    |     4795   | 2569   | 12548     |
| 7    |   5000    |     4990   | 2569   | 12548     |
| 8    |   5800    |     5795   | 2569   | 12548     |
| 9    |   7500    |     7950   | 2569   | 12548     |
| 10   |   8300    |     8495   | 2569   | 12548     | 

我想在表格的最后两列显示每个数据库表的最后一条记录和echo kh_hokh_ha

您可以使用 PHP MySQL 来帮助我吗?

3 个答案:

答案 0 :(得分:6)

如何使用此单行查询?

SELECT * FROM MyTable ORDER BY id  DESC LIMIT 1;

完整的PHP代码:

好吧,我不是一个PHP人,但这应该有所帮助:

    <?php
        $dbc = mysqli_connect('localhost','test','test','test') or die('Error connecting to MySql Server.');
        $queryShowTable = "SHOW TABLES;";
        $querySelect_Template = "SELECT kh_ho, kh_ha FROM ";
        $listOftables = mysqli_query($dbc,$queryShowTable) or die('Error querying database.');
        echo '<table>';
        while($table=mysqli_fetch_array($listOftables)){
            $querySelectData = $querySelect_Template.$table[0].' ORDER BY id DESC LIMIT 1;';          
            $tableData = mysqli_query($dbc,$querySelectData) or die('Error querying database.');
            while($row = mysqli_fetch_array($tableData)){
                echo '<tr><td>'.$row[0].'</td><td>'.$row[1].'</tr>';
            }                
        }
        echo '</table>';
        mysqli_close($dbc);            
    ?>

答案 1 :(得分:4)

由于最后一条记录的ID最高,

 SELECT * FROM `yourtable` WHERE `id` = (select max(id) from  `yourtable`);

使用也可以使用order by子句

SELECT * FROM `yourtable` ORDER BY id  DESC LIMIT 1;

获取所有表格

SELECT * FROM `firsttable` WHERE `id` = (select max(id) from  `firsttable`)
UNION ALL
SELECT * FROM `secondtable` WHERE `id` = (select max(id) from  `secondtable`) 
UNION ALL
SELECT * FROM `thirdtable` WHERE `id` = (select max(id) from  `thirdtable`)
.
.
.
UNION ALL
SELECT * FROM `lasttable` WHERE `id` = (select max(id) from  `lasttable`);          

Union all将确保为不同的表列出相同的条目

EDIT2

<?php

  $sql = 'show tables';
  $result = mysql_query($sql);
  $numofrows = mysql_num_rows($result);

    $sqlQuery = '';
    $i=0;
    while($d = mysql_fetch_array($result)){
        $i++;

        if($i==$numofrows){
          $sqlQuery .= "SELECT * FROM `".$d[0]."` WHERE `id` = (select max(id) from  `".$d[0]."`)" ;
        }else{
          $sqlQuery .= "SELECT * FROM `".$d[0]."` WHERE `id` = (select max(id) from  `".$d[0]."`) UNION ALL " ;
        }
    }

    echo $sqlQuery; //your Sql Query

?>

答案 2 :(得分:1)

首先,您需要触发一个查询:

SHOW TABLES;

结果你将获得所有表格。 现在使用PHP的foreach()

foreach($resultset AS $k=>$v)
{
  // fire query over here:
  //i.e. SELECT * FROM `$v['tablename']` ORDER BY id  DESC LIMIT 1;
}

这只是一个想法。