Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

mysql - how to insert records for top 10 enteries only

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");

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.

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

Kindly guide.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a JOIN for this purpose like

UPDATE `tablename` a 
JOIN 
(
SELECT `userid`, SUM(`points`) as `total` FROM 
    `tablename` GROUP BY `userid` ORDER BY total DESC LIMIT 10
) tab ON a.`userid` = tab.`userid`       
set a.Bonus = 2000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...