English 中文(简体)
MySQL 计算顺序, 然后根据结果更新表格
原标题:MySQL Calculate rank then update table from a result
  • 时间:2012-05-24 12:19:06
  •  标签:
  • mysql
  • rank

我的问题是,我试图计算所有用户条目,然后排行。在我排行它们之后,我想更新用户表,记录每个用户的排行。

表:

用户Tbl

活动图示

我已经找了整晚了 这是我目前为止有的

SET @rank := 0;
SELECT * FROM (
SELECT @rank := @rank + 1 AS rank, a.userName
FROM 活动图示 a
LEFT JOIN 用户Tbl u ON a.userName = u.userName
GROUP BY a.userName
ORDER BY SUM( TIME_TO_SEC( a.actTime ) )DESC
) as sub

此返回 :

用户级别@ user

1 Kim 1 Kim

2 约翰·约翰·约翰

3 Joe 3 Joe( 3 Joe)

This is fine, but I ve tried to add Update and Set to this query to update the 用户Tbl and I just can t get it to work.

需要更多信息请告诉我

更新:

对于那些寻找 准确什么 我做了纠正我的问题。

首先,我贴错了 MySQL。 我用来解决我问题的一个是:

SET @rownum := 0;
SELECT @rownum := @rownum + 1 AS rank, userName, actTimeTotal 
FROM (SELECT SUM(TIME_TO_SEC(actTime)) AS actTimeTotal , userName 
FROM 活动图示 
GROUP BY userName 
ORDER BY actTimeTotal DESC) as result

第二,我创建了一个名为排名Tbl的排名表,有两个栏:小Int和用户Name varchar。

第三,修改 MySql 脚本为 :

SET @rownum := 0;
INSERT INTO rankTbl( rank, userName)
SELECT @rownum := @rownum + 1 AS rank, userName 
FROM (SELECT SUM(TIME_TO_SEC(actTime))AS actTimeTotal , userName 
FROM 活动图示 
GROUP BY userName 
ORDER BY actTimeTotal DESC) as result
ON DUPLICATE KEY UPDATE userName = VALUES(userName);

我将 On DUPIATE KEY 更新为用户名, 而不是级别。 当级别改变时, 级别没有更新用户名 。 使用用户名更新级别变化 。

最佳回答

您需要做的是确保您的排名表对用户有主密钥,然后用 KEY 错误条款发布 SELECT 的 INSERT :

总和您的表格名称为 tbl , 包含一个 rank 字段和一个 user 字段, 我基本上要说的是, 您需要以下查询来返回所有行的 < strong > 重复关键字错误 :

SET @rank := 0;
INSERT INTO tbl (rank, user)
    SELECT @rank := @rank + 1 AS rank, a.userName
    FROM activityTbl a
    LEFT JOIN userTbl u ON a.userName = u.userName
    GROUP BY a.userName
    ORDER BY SUM(TIME_TO_SEC(a.actTime)) DESC;

一旦您有了该选项, 并且您可以确定它要插入的所有行返回一个 < 坚固 > 重复的密钥错误 , 您就会修改如下:

SET @rank := 0;
INSERT INTO tbl (rank, user)
    SELECT @rank := @rank + 1 AS rank, a.userName
    FROM activityTbl a
    LEFT JOIN userTbl u ON a.userName = u.userName
    GROUP BY a.userName
    ORDER BY SUM(TIME_TO_SEC(a.actTime)) DESC
ON DUPLICATE KEY UPDATE rank = VALUES(rank);
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签