Mysql每月从三个表中获取数据

时间:2017-11-11 03:30:50

标签: mysql database

我有三张表,其中包含2006年至2017年的数据

我想每月从表中获取数据,比如我有用户,比赛和电影表,

我想获取用户每个月注册的数据以及当月运行的比赛数量以及参加比赛的数量

我试过

def reOrder(list1,list2):
Found = False
if len(list1)!=len(list2):
    return False
for e in list2:
    if e not in list1:
        return False
return True     
print(reOrder([1,7,10,8,4,2], [3,1,8,10,2,4]))

但不适合我。

用户表

SELECT DATE_FORMAT(t.user_regdate, "%Y-%m") AS "_Month", COUNT(*)
FROM users as t
GROUP BY _Month,   
SELECT  COUNT(*) as films , (select c.name from contest as c where c.contest_id =f.contestid) as contest
FROM film as f GROUP BY contestid

电影表

----------------------------------
|user_id | user_regdate| username| 
----------------------------------
|5135    | 1164697208  | test    |  
----------------------------------

比赛表

-------------------------------------------------------------
|film_id |     date_created   | title| contestid |  User_id |
----------------------------------
|5135    | 2017-09-23 00:45:26| test |    1     |   5135    |
--------------------------------------------------------------

我想要

2006-12 | -count(用户)|当月比赛名称|电影提交比赛|

1 个答案:

答案 0 :(得分:0)

#For per month registered user count

select DATE_FORMAT(t.user_regdate, "%Y-%m") AS '_Month', COUNT(*) from users  group by DATE_FORMAT(t.user_regdate, "%Y-%m");

# per contest film count not used month there 
SELECT  contestid,COUNT(*) as films , (select c.name from contest as c where c.contest_id =f.contestid) as contest
FROM film as f GROUP BY contestid;

# per contest film count with month 

SELECT  contestid,COUNT(*) as films ,DATE_FORMAT(t.date_created, "%Y-%m") AS '_Month', (select c.name from contest as c where c.contest_id =f.contestid) as contest
FROM film as f GROUP BY contestid,DATE_FORMAT(t.date_created, "%Y-%m");

# per User film count total

SELECT  user_id,COUNT(*) as films , (select c.username from user_id as c where c.user_id =f.user_id) as UserName
FROM film as f GROUP BY user_id;

# per User film count in month

SELECT  user_id,COUNT(*) as films,DATE_FORMAT(t.date_created, "%Y-%m") AS '_Month' , (select c.username from user_id as c where c.user_id =f.user_id) as UserName
FROM film as f GROUP BY user_id,DATE_FORMAT(t.date_created, "%Y-%m");

也许这些查询可以帮助你......