如何从mysql结果中选择第一个和最后一个数据行? [英] How to select first and last data row from a mysql result?

查看:906
本文介绍了如何从mysql结果中选择第一个和最后一个数据行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT * from User返回75个用户.是否可以选择第1个用户和第75个用户而不执行while($row = mysql_fetch_assoc($result))?以及如何?

SELECT * from User returns 75 users. Is it possible to select 1st user, and 75th user without doing while($row = mysql_fetch_assoc($result)) ?? and how?

更新

请更加清楚:在执行while mysql_fetch_assoc之前,我需要拥有SELECT *,因为我需要第一个用户和第75个用户,因此不需要ASC, DESC, LIMIT答案.

Just to be more clear: I need to have SELECT * as I need the first and 75th user before I do while mysql_fetch_assoc so ASC, DESC, LIMIT answers not required.

推荐答案

SELECT * from User LIMIT 1
UNION
SELECT * from User LIMIT 74,1


修改

@Kay:创建结果集后,PHP无法更改其内部顺序.

@Kay: PHP can't change the internal order of the resultset after it's created.

如果查询总是返回75行,那么访问其他1和75的唯一方法就是使用

If the query always returns 75 rows then the only way to access the 1st and the 75th before anything else would be to use mysql_data_seek which moves the internal result pointer:

$result = mysql_query('SELECT * from User');

mysql_data_seek($result, 1);
$row1 = mysql_fetch_assoc($result);

mysql_data_seek($result, 75);
$row75 = mysql_fetch_assoc($result);

请注意,如果上述内容后接while,则必须将指针重置到合适的位置.

Note that if the above is followed by a while, the pointer must be reset to a suitable position.

这篇关于如何从mysql结果中选择第一个和最后一个数据行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆