获得最高分和胜利数

时间:2016-04-10 21:14:29

标签: mysql select join

Database

这是我的数据库。每张照片都有一些点,由特定用户拍摄。每张照片也分配给该活动。我想找到每个事件点数最多的图片。我还想计算你赢了多少个事件(例如,对于id = 10的用户)。请你帮助我好吗?我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

以下两个查询可以满足您的需求:

  

每个事件的点数最多的图片。

package com.xxx.xxx;
import android.app.Application;

public class Global extends Application {

    private static Global instance; 

    public Global() { 
        instance = this; 
    } 

    private final String PACKAGE="com.webnews.appdirector";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public String getPackageName() {
        return PACKAGE;  //this work fine, but how to know if is webkit callback ??? "instance" have mcomponentcallbacks set to webkit, but how to read it ???
    }
}

请参阅SQL fiddle

  

计算您赢得的赛事数量

SELECT      e.id event_id,
            e.name event_name, 
            ph.id photo_id,
            ph.title photo_title,
            u.id user_id,
            u.login user_login,
            COUNT(*) points
FROM        events e
INNER JOIN  photos ph
        ON  ph.event_id = e.id
        AND ph.id = (
              SELECT      ph.id
              FROM        photos ph
              INNER JOIN  points p 
                       ON p.photo_id = ph.id
              WHERE       ph.event_id = e.id
              GROUP BY    ph.id
              ORDER BY    COUNT(*) DESC
              LIMIT       1
            )
-- optional if you need to know the points            
INNER JOIN  points p 
         ON p.photo_id = ph.id
-- optional if you need to know the owner of the photo
INNER JOIN  users u
         ON u.id = ph.user_id
GROUP BY    e.id,
            e.name,
            ph.id,
            ph.title

请参阅SQL fiddle