如何仅插入前 10 个条目的记录 [英] how to insert records for top 10 enteries only

查看:45
本文介绍了如何仅插入前 10 个条目的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为特定任务收集了前 10 位用户.使用下面给出的查询.

I have gathered top 10 users for specific task. Using the query given below.

$sqlsum=mysql_query("SELECT `userid`, SUM(`points`) as `total` FROM 
    `tablename` GROUP BY `userid` ORDER BY total DESC LIMIT 10");

现在,我只需要奖励前 10 名聚集的用户.我不明白如何为此目的编写查询.

Now , I need to award bonues to the top 10 gathered users only. I am not getting the idea , on how to write query for this purpose.

我需要更新 tablename = user , field name = bonus 中的一个字段.

I need to update one field in tablename = user , field name = bonus .

请指导.

推荐答案

你可以试试:

$query = "UPDATE user 
          SET bonus=$some_bonus 
          WHERE id IN (
          SELECT userid from (select userid,sum(points) as total 
          FROM table_name GROUP BY userid ORDER BY total DESC LIMIT 10))";

这里的 id 是给用户的 user 表的 id,$some_bonus 是你要奖励给前 10 名用户的奖金.

here id is the id of the user table given to users and $some_bonus will be the bonus you want to award to the top 10 users.

这篇关于如何仅插入前 10 个条目的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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