获取用户游戏记录.sql 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. SELECT
  2. id, nickname,
  3. if(gender = 2, '女', '男') as gender,
  4. phone, birthday,
  5. integral, coin, diamond, mac,
  6. country, state, city,
  7. if (offlineTime > 0, FROM_UNIXTIME(offlineTime / 1000, '%Y-%m-%d %H:%i'), '') as offlineTime,
  8. CEIL(totalGameDuration / 60) 'totalGameDuration', totalGameCount,
  9. date,
  10. CEIL(dateGameDuration / 60) 'dateGameDuration', dateGameCount,
  11. CEIL(game1_duration / 60) 'game1_duration', game1_count,
  12. CEIL(game2_duration / 60) 'game2_duration', game2_count,
  13. CEIL(game3_duration / 60) 'game3_duration', game3_count,
  14. time, gameType, CEIL(duration / 60) 'duration'
  15. FROM
  16. (
  17. (
  18. user_info #表1-用户信息
  19. LEFT JOIN
  20. (
  21. SELECT userID, sum(duration) as totalGameDuration, count(*) as totalGameCount
  22. FROM user_game_record
  23. GROUP BY userID
  24. ) total_game_record #表2-用户总游戏记录
  25. ON user_info.id = total_game_record.userID
  26. )
  27. LEFT JOIN
  28. (
  29. SELECT
  30. userID,
  31. date_format(dateTime, '%Y-%m-%d') as date,
  32. sum(duration) as dateGameDuration, count(*) as dateGameCount,
  33. sum(if(gameType in (1), duration, 0)) as game1_duration,
  34. count(if(gameType in (1), TRUE, null)) as game1_count,
  35. sum(if(gameType in (3,4,5), duration, 0)) as game2_duration,
  36. count(if(gameType in (3,4,5), TRUE, null)) as game2_count,
  37. sum(if(gameType in (2,6,7,8,9,10,11,12), duration, 0)) as game3_duration,
  38. count(if(gameType in (2,6,7,8,9,10,11,12), TRUE, null)) as game3_count
  39. FROM user_game_record
  40. GROUP BY userID, date_format(dateTime, '%Y-%m-%d')
  41. ) date_game_record #表3-用户某日游戏记录
  42. ON user_info.id = date_game_record.userID
  43. )
  44. LEFT JOIN
  45. (
  46. SELECT userID, date_format(dateTime, '%Y-%m-%d') as date0, date_format(dateTime, '%H:%i') as time, gameType, duration
  47. FROM user_game_record
  48. ) time_game_record #表4-用户某日时间游戏记录
  49. ON date_game_record.userID = time_game_record.userID AND date_game_record.date = time_game_record.date0
  50. ORDER BY totalGameDuration DESC, id, date DESC, time