[英]Select last record of each table on database
I would like to show the last record of each table from my database . 我想显示数据库中每个表的最后一条记录。 All tables have the same structure like the ones below:
所有表都具有与以下相同的结构:
+------+-----------+------------+--------+-----------+
| 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 |
I wanted to show the last record of each database table and echo kh_ho
and kh_ha
on the last two column in a table. 我想显示每个数据库表的最后一条记录,并在表的最后两列上回
kh_ho
和kh_ha
。
Can you help me on this by using PHP and MySQL ? 您可以使用PHP和MySQL帮助我吗?
how about using this single line query? 如何使用此单行查询?
SELECT * FROM MyTable ORDER BY id DESC LIMIT 1;
Full PHP Code: 完整的PHP代码:
Well, I'm not a PHP guy but this should help: 好吧,我不是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);
?>
Since the last record will have highest id , 由于最后一条记录的ID最高,
SELECT * FROM `yourtable` WHERE `id` = (select max(id) from `yourtable`);
Use can use order by clause also 使用也可以使用order by子句
SELECT * FROM `yourtable` ORDER BY id DESC LIMIT 1;
For getting all table entry 用于获取所有表条目
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 Will ensure that same entry are listed for different table 全部合并将确保为不同的表列出相同的条目
Edit2 编辑2
<?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
?>
First of all you need to fire one query: 首先,您需要触发一个查询:
SHOW TABLES;
In result you will get all tables. 结果,您将获得所有表。 Now using foreach() of PHP
现在使用PHP的foreach()
foreach($resultset AS $k=>$v)
{
// fire query over here:
//i.e. SELECT * FROM `$v['tablename']` ORDER BY id DESC LIMIT 1;
}
It is just an idea. 这只是一个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.