1

在 MySQL 中,我需要按位置获取最高分的艺术家。请帮忙。

user_location_tbl(用户位置表)

|--------------------------------------|
| countryLat | countryLong  |  userid  |
|--------------------------------------|
| 31.695766  | 54.624023    |  1       |
| 20.593684  | 78.96288     |  2       |
| 20.593684  | 78.96288     |  3       |
| 20.593684  | 78.96288     |  4       |
| 31.695766  | 54.624023    |  5       |
|--------------------------------------|

Fans_table(查看哪个艺术家拥有哪个用户作为粉丝)

|----------------------|----------
| artist_id  | user_id |  Points |   
|----------------------|----------
| 1          | 1       |    20    |
| 1          | 2       |    30    |
| 2          | 1       |    40    |
| 2          | 3       |    40    |   
| 3          | 1       |    60    |
|----------------------|----------

艺术家表(艺术家列表)

|-------------------|
| artistid | name   |
|-------------------|
| 1        | raja   | 
| 2        | sekar  |
| 3        | thomas |
|-------------------|

我需要知道哪个艺术家的位置最高,但我不能在一个查询中做到这一点。如果我将 sum(points) 和按 countryLat、countryLong、artistid 分组,我会得到以下结果......

|---------------------------------|
| 100 | 20.593684 | 78.96288  | 1 |
| 50  | 20.593684 | 78.96288  | 2 |
| 100 | 31.695766 | 54.624023 | 3 |
| 90  | 31.695766 | 54.624023 | 1 |
|---------------------------------|

...但我只需要在该位置具有最高点的艺术家,如下例所示...

|---------------------------------|
| 100 | 20.593684 | 78.96288  | 1 |
| 100 | 31.695766 | 54.624023 | 3 |
|---------------------------------|

SQL

SELECT Sum(c.tot_points), 
       a.artist_id 
FROM   `fans_table` AS a 
       INNER JOIN `user_location_tbl` AS b 
               ON a.user_id = b.user_id 
       INNER JOIN artist_table AS c 
               ON a.artist_id = c.id 
GROUP  BY b.countrylat, 
          b.countrylong, 
          a.artist_id 

以上是我的查询

4

2 回答 2

1

Making an assumption that the points in an area will be unique (ie, either there won't be 2 equally popular artists in an area, or if there are you want them both) then something like the following will do it

SELECT Sub3.artist_id, Sub3.countryLat, Sub3.countryLong, Sub2.MaxArtistLocalPoints
FROM
(
    SELECT a.artist_id, countryLat, countryLong, SUM(a.Points) AS ArtistLocalPoints
    FROM `fans_table` AS a 
    INNER JOIN  `user_location_tbl` AS b ON a.user_id=b.user_id 
    INNER JOIN artist_table AS c ON a.artist_id=c.id 
    GROUP BY a.artist_id, countryLat, countryLong
) Sub3
INNER JOIN
(
    SELECT countryLat, countryLong, MAX(ArtistLocalPoints) AS MaxArtistLocalPoints
    FROM
    (
        SELECT a.artist_id, countryLat, countryLong, SUM(a.Points) AS ArtistLocalPoints
        FROM `fans_table` AS a 
        INNER JOIN  `user_location_tbl` AS b ON a.user_id=b.user_id 
        INNER JOIN artist_table AS c ON a.artist_id=c.id 
        GROUP BY a.artist_id, countryLat, countryLong
    ) Sub1
) Sub2
ON Sub3.countryLat = Sub2.countryLat
AND Sub3.countryLong = Sub2.countryLong
AND Sub3.ArtistLocalPoints = Sub2.MaxArtistLocalPoints
于 2013-06-28T13:12:28.717 回答
1

因此,假设我正确理解您的问题,您想知道哪个艺术家在每个位置上得分最高,因此这将是手动计算的过程:

// First we add up all the points for each artist in each location:
|----------------------------------------------|
| Points | countryLat | countryLong | artistid |
|----------------------------------------------|
|    30  |  20.593684 |  78.96288   |     1    |
|    40  |  20.593684 |  78.96288   |     2    |
|     0  |  20.593684 |  78.96288   |     3    |
|    20  |  31.695766 |  54.624023  |     1    |
|    40  |  31.695766 |  54.624023  |     2    |
|    60  |  31.695766 |  54.624023  |     3    |
|----------------------------------------------|

// Then we get the max for each location
|----------------------------------------------|
| Points | countryLat | countryLong | artistid |
|----------------------------------------------|
|    40  |  20.593684 |  78.96288   |     2    |
|    60  |  31.695766 |  54.624023  |     3    |
|----------------------------------------------|

执行此操作的查询如下

SELECT artist_id, ul.countryLat, ul.countryLong, max_points
FROM fans_table f
JOIN user_location_tbl ul
ON ul.userid = f.user_id
JOIN (
    SELECT countryLat, countryLong, MAX(f.total_points) max_points
    FROM
        (SELECT artist_id, countryLat, countryLong, SUM(Points) as total_points
        FROM fans_table f
        JOIN user_location_tbl ul
        ON ul.userid = f.user_id
        GROUP BY artist_id, countryLat, countryLong) f
    GROUP BY countryLat, countryLong) p
ON p.countryLat = ul.countryLat
AND p.countryLong = ul.countryLong
GROUP BY f.artist_id, ul.countryLat, ul.countryLong
HAVING p.max_points = SUM(Points)
于 2013-06-28T13:19:25.727 回答