简体   繁体   English

MySQL 根据表的 int 字段选择前 10 条记录

[英]MySQL selecting Top 10 Records Based on an int field of a table

i have a table called Book that has a field(BorrowTime) that will increment once it's being borrowed.我有一个名为 Book 的表,它有一个字段(BorrowTime),一旦被借用,它就会增加。 something like below像下面这样的东西

i want to retrieve the top 10 records with the highest borrowtime.我想检索借用时间最长的前 10 条记录。 my sql statement:我的sql语句:

$results = mysql_query("SELECT * from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_array($results))
{
   echo $row['Title'];
}

it's not working and have infinite loop.它不起作用并且有无限循环。 does anyone know what goes wrong?有谁知道出了什么问题?

no problem with your code, but try debuging using this script,您的代码没有问题,但请尝试使用此脚本进行调试,

$results = mysql_query("SELECT * from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_assoc($results))
{
    print_r($row);
    exit;//force exit script to debuging, are we get correct result or not
}

Change your code to:将您的代码更改为:

$results = mysql_query("SELECT Title from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_array($results)){
   echo $row['Title'];
}

Because you are only using the field 'Title', and no other fields from that table.因为您只使用字段“标题”,而没有使用该表中的其他字段。

Also, when this field is defined as 'title' than $row['Title'] wont show anything, because you should than use $row['title']此外,当这个字段被定义为 'title' 时, $row['Title'] 不会显示任何内容,因为你应该使用 $row['title']

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

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