我该如何进行此查询?

时间:2011-08-08 11:16:43

标签: php mysql sql

这是测试表

----------------------------------------------
id | username | point | level | created_date
----------------------------------------------
 1 | name_a   |  1    |   1   | 2011-08-01
 2 | name_a   |  2    |   2   | 2011-08-02
 3 | name_b   |  5    |   1   | 2011-08-02
 3 | name_c   |  6    |   1   | 2011-08-02
 4 | name_d   |  1    |   1   | 2011-08-01
 5 | name_d   |  3    |   1   | 2011-08-02
 5 | name_d   |  5    |   2   | 2011-08-03
 4 | name_e   |  5    |   1   | 2011-08-01
 5 | name_e   |  5    |   2   | 2011-08-02
 5 | name_e   |  5    |   3   | 2011-08-03
----------------------------------------------

查询的要求是查询(尽可能在一个查询中)用户名,表的点。

  1. 按每个级别的用户得分总和排序。
  2. 如果用户在同一级别获得2分,则只能获得最新分数。
  3. 按用户名分组
  4. 总分必须小于10
  5. 每个级别的得分最高为5
  6. 输出样本:

    --------------------
     username  | tpoint|
    --------------------
      name_d   |  8    |
      name_b   |  5    |
      name_a   |  3    |
    --------------------
    

    name_ename_c被忽略。

4 个答案:

答案 0 :(得分:7)

听起来像一个有趣的查询!

SELECT username, SUM(point) AS points
FROM (SELECT username, level, point
      FROM (SELECT username, level, LEAST(point, 5) AS point
            FROM table
            WHERE points <= 5
            ORDER BY created_date DESC) AS h
      GROUP BY username, level) AS h2
GROUP BY username
HAVING points < 10
ORDER BY points DESC

应该这样做!只需替换“table”。

编辑:

是否要排除得分超过5或值为5的行?只需删除WHERE点&lt; = 5,如果是这样。

答案 1 :(得分:2)

SELECT SUM(t3.point) AS tpoint, t3.username
FROM (
    SELECT t1.level, t1.username, t1.created_date, t1.point
    FROM testing AS t1
    INNER JOIN (SELECT level, username, MAX(created_date) AS MaxDate
                FROM testing) AS t2
          ON (t1.level=t2.level AND t1.username=t2.username AND t1.created_date = t2.MaxDate)
    WHERE t1.point <= 5
    ) AS t3
GROUP BY t3.username
HAVING tpoint < 10
ORDER BY tpoint DESC

不知道我是否正确使用了别名,希望这有效!

使用联接的内部查询是获取最新的用户名,级别组合,其中单级点数是&gt; 5.然后用它来获取每个用户名的总和,并丢弃那些超过10个点的用户。

答案 2 :(得分:2)

SELECT 
    Query2.username 
  , Sum(Query2.SomVanpoint) AS point 
FROM 
    (SELECT 
            test.username 
          , test.level 
          , Sum(test.point) AS SomVanpoint 
        FROM 
            test 
        INNER 
        JOIN 
            (SELECT 
                    test.username 
                  , test.level 
                  , Max(test.created_date) AS MaxVancreated_date 
                FROM 
                    test 
                GROUP 
                    BY test.username 
                  , test.level
            ) AS Query1 
            ON 
            (test.username         = Query1.username) 
            AND (test.level        = Query1.level) 
            AND (test.created_date = Query1.MaxVancreated_date) 
        GROUP 
            BY test.username 
          , test.level 
        HAVING 
            (((Sum(test.point))<= 5))
    ) AS Query2 
GROUP 
    BY Query2.username 
HAVING 
    (((Sum(Query2.SomVanpoint))< 10)) 
ORDER 
    BY Sum(Query2.SomVanpoint) DESC; 

===输出:

username  | point
----------+------
name_d    |  8
name_b    |  5
name_a    |  3

答案 3 :(得分:1)

好的,先参加第2部分......

SELECT *
FROM table a
WHERE NOT EXISTS (
   SELECT 1
   FROM table b
   WHERE b.username=a.username
   AND a.created_date>b.created_date
)

但是mysql并不能很好地处理推送谓词,因此最大的连接技巧,但这将使查询变得非常复杂 - 如果你遇到性能问题值得重新审视。现在添加其他东西....部分1,3和5

SELECT username, level, SUM(point)
FROM 
(SELECT *
   FROM table a
   WHERE NOT EXISTS (
      SELECT 1
      FROM table b
      WHERE b.username=a.username
      AND a.created_date>b.created_date
   )
) ilv
GROUP BY username, level
HAVING SUM(point) <= 5;

如何实现4取决于此约束相对于其他约束(特别是2和5)应用的确切顺序。以下内容应该从所述输入中给出disred输出......

SELECT username, level, SUM(point)
FROM 
(SELECT *
   FROM table a
   WHERE NOT EXISTS (
      SELECT 1
      FROM table b
      WHERE b.username=a.username
      AND a.created_date>b.created_date
   )
) ilv,
(SELECT username, SUM(point) as totpoint
  FROM table c
  GROUP BY username
  HAVING SUM(point)<=10) ilv2
WHERE ilv.username=ilv2.username
GROUP BY username, level
HAVING SUM(point) <= 5;

糟糕 - 再次阅读本文,看到你对输出集中的级别细分不感兴趣 - 在这种情况下,罗宾的答案更好。