在PHP中使用SQL语句 怎么取出查询出来的最后一个数据?

<?php$con = mysql_connect("127.0.0.1","root","pi2013wik") or die("Could not connect");mysql_select_db("tongji",$con);mysql_query("set name 'utf8'");$sql ="select a.name name1,a.*,b.value,b.* from piwik_site a,piwik_archive_numeric_2013_10 b where b.period in(1,2,3) and b.name='nb_visits' and a.idsite=b.idsite order by b.value desc";$res = mysql_query($sql);?>
<?phpwhile($row = mysql_fetch_array($res)){?> <tr>
<td bordercolor="#333333" bgcolor="#CCCCCC"><div align="left"><?php echo $row['name1']?></div></td> <td bordercolor="#333333" bgcolor="#CCCCCC"><div align="left"><?php echo $row['date1']?></div></td> <td bordercolor="#333333" bgcolor="#CCCCCC"><div align="left"><?php if($row['period'] == 1){echo $row['value'];}?></div></td> <td bordercolor="#333333" bgcolor="#CCCCCC"><div align="left"><?php if($row['period'] == 2){echo $row['value'];}?></div></td> <td bordercolor="#333333" bgcolor="#CCCCCC"><div align="left"><?php if($row['period'] == 3){echo $row['value'];}?></div></td> </tr> <?php }?>
以上都是都是正确的,下面是查询出来的数据

我现在想查询出来 每天的最后一个数据 每周的最后一个数据(26791) 怎么 能实现 这个SQL和输出的时候应该怎么写??高手回答!

在PHP中使用SQL语句可以通过倒序排列记录取出第一条的记录取到最后一条数据。
一般,php调用mysql的接口查询,查询语句如下:
select * from table order by id DESC limit 1
这样就取出记录的最后一条记录。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-28
倒排查出第一个或者count()你得到的结果 然后减1就是你最后一条的索引本回答被提问者采纳
第2个回答  2015-09-29
select a.name name1,a.*,b.value,b.* from piwik_site a,piwik_archive_numeric_2013_10 b where b.period in(1,2,3) and b.name='nb_visits' and a.idsite=b.idsite order by b.value desc

如果页面显示的是这个语句,那么最后一个应该是select a.name name1,a.*,b.value,b.* from piwik_site
a,piwik_archive_numeric_2013_10 b where b.period in(1,2,3) and
b.name='nb_visits' and a.idsite=b.idsite order by b.value asc limit 1
或者先执行一次select count(*) 拿到数量num,再select a.name name1,a.*,b.value,b.* from piwik_site
a,piwik_archive_numeric_2013_10 b where b.period in(1,2,3) and
b.name='nb_visits' and a.idsite=b.idsite order by b.value desc limit num-1,1
第3个回答  2013-11-28
循环语句有错误,追问

请问错在那? 应该怎么写?

第4个回答  2013-11-28
queue 按id从大到小,取第一条

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网