简体   繁体   English

Mysql创造了前10名

[英]Mysql creating a top 10

I have big database full of information and I use the following Query to select the information I need. 我有大量的信息数据库,我使用以下查询来选择我需要的信息。

$query = mysql_query("SELECT * FROM db WHERE model LIKE '%iphone 4%' AND model LIKE '%16GB%'") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {

This leaves me with about 250 results, I use a while loop and display a [tr] for each result. 这给我留下了大约250个结果,我使用while循环并为每个结果显示[tr]。 Now I want to create a top ten. 现在,我想创建一个前十名。 This is what I came up with: 这就是我想出的:

$monthavg = ($row['price_shipping'] / $row['abo_time']) + ($row['price'] / $row['abo_time']) + $row['abo_price'];

So now I can echo $monthavg and its a good way to get a top ten. 所以现在我可以回应$ monthavg,这是获得前十名的好方法。 What I would rather like to do is just show 10 rows [tr] with the lowest $monthavg. 我想做的就是只显示$ monthavg最低的10行[tr]。 But I already echo'd a table to the screen. 但是我已经在屏幕上回荡了一张桌子。

My question: Can I create a top 10 of my database and echo just those 10 results? 我的问题:我是否可以创建数据库的前10位并回显这10个结果?

You already have the answer, just redo the query with your calculation as column, then order by that column and limit it to first 10 results 您已经有了答案,只需将计算作为列重做查询,然后按该列排序并将其限制为前10个结果

SELECT *, (price_shipping / abo_time + price / abo_time + abo_price) AS month_avg 
FROM db WHERE model LIKE '%iphone 4%' AND model LIKE '%16GB%'
ORDER BY month_avg ASC LIMIT 10

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

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