SELECT列FROM t1,在t2中与columnS的COUNT连接,在t3中与COUNT连接

时间:2014-07-22 23:30:19

标签: mysql

我正在尝试将来自3个表的数据组合如下:

    SELECT u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE, 
    u.ISADMIN, COUNT(m.PURCHASED_ID) AS MOVIES_PURCHASED, 
    COUNT(r.RENTED_ID) AS MOVIES_RENTED

    FROM TBL_USERS AS u 
    LEFT JOIN TBL_MOVIE_PURCHASED AS m 
    ON u.USER_ID = m.USER_ID 

    LEFT JOIN TBL_RENTED_MOVIES AS r 
    ON u.USER_ID = r.USER_ID 

    GROUP BY u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE,u.ISADMIN
    ORDER BY MOVIES_PURCHASED, MOVIES_RENTED;

表格包含以下列:

TBL_USERS : 
USER_ID, USERNAME, FIRSTNAME, LASTNAME, ISACTIVE, ISADMIN

TBL_MOVIE_PURCHASED :
USER_ID, MOVIE_ID, PURCHASE_ID, PURCHASED_ON (purchase ID is unique)

TBL_RENTED_MOVIES : 
USER_ID, MOVIE_ID, RENTED_ID, RENTED_ON (rented ID is unique )

我想显示:

1.  all contents of TBL_USERS 
2.  the count of the rented movies for every user from TBL_RENTED_MOVIES,
3.  the count of the purchased movies for every user from TBL_MOVIES_PURCHASED.
4.  ORDER BY the results based on both COUNTS.  

如果说user1租了5部电影并购买了10部电影,那么该查询应该返回:

(MOVIES_PURCHASED, MOVIES_RENTED) = (5,10). 

相反,对于两列,查询返回5 * 10 = 50:

(MOVIES_PURCHASED, MOVIES_RENTED) = (50,50)

我知道我在加入结果时出错了。我尝试使用UNION来合并结果但是没有用。有任何想法吗?

因此,输出应理想地为:

USER_ID, USERNAME, FIRST_NAME, LAST_NAME, ISACTIVE, ISADMIN, MOVIES_PURCHASED, MOVIES-RENTED :
1, user1, userFirst, userLast, Active, NotAdmin, 5, 10

。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

这是一种方式......

CREATE table USERS(USER_ID integer primary key, 
                       USERNAME varchar(50), FIRST_NAME varchar(50),
                       LAST_NAME varchar(50), ISACTIVE VARCHAR(10)
                       DEFAULT '0',
                       ISADMIN VARCHAR(10) DEFAULT '0');

CREATE table MOVIES_PURCHASED(PURCHASE_ID integer primary key, 
                               USER_ID integer,
                               FOREIGN KEY(USER_ID) REFERENCES USERS(USER_ID), 
                               PURCHASED_ON VARCHAR(50));

CREATE table MOVIES_RENTED(RENTED_ID integer primary key, 
                               USER_ID integer,
                               FOREIGN KEY(USER_ID) REFERENCES USERS(USER_ID), 
                               RENTED_ON VARCHAR(50));





INSERT INTO USERS (USER_ID, USERNAME, FIRST_NAME, LAST_NAME, ISACTIVE, ISADMIN)
VALUES
(1, 'user1', 'user1FN', 'user1LN', '1', '0'),
(2, 'user2', 'user2FN', 'user2LN', '1', '0'),
(3, 'user3', 'user3FN', 'user3LN', '1', '0'),
(4, 'user4', 'user4FN', 'user4LN', '1', '0'),
(5, 'user5', 'user5FN', 'user5LN', '1', '0');


insert into MOVIES_RENTED  (RENTED_ID, USER_ID, RENTED_ON) 
values 
(1, 5 ,'2014-07-05'),
(2, 4 ,'2014-07-05'),
(3, 5 ,'2014-07-05'),
(4, 4 ,'2014-07-05'),
(5, 5 ,'2014-07-05'),
(6, 5 ,'2014-07-05'),
(7, 3 ,'2014-07-05'),
(8, 2 ,'2014-07-05'),
(9, 2 ,'2014-07-05'),
(10, 1 ,'2014-07-05');


insert into MOVIES_PURCHASED  (PURCHASE_ID, USER_ID, PURCHASED_ON) 
values 
(1, 1 ,'2014-07-05'),
(2, 3 ,'2014-07-05'),
(3, 3 ,'2014-07-05'),
(4, 3 ,'2014-07-05'),
(5, 4 ,'2014-07-05'),
(6, 4 ,'2014-07-05'),
(7, 5 ,'2014-07-05'),
(8, 5 ,'2014-07-05'),
(9, 1 ,'2014-07-05'),
(10, 2 ,'2014-07-05'),
(11, 2 ,'2014-07-05'),
(12, 2 ,'2014-07-05');

SELECT u.*
     , SUM(x.source = 'rented') rented
     , SUM(x.source = 'purchased') purchased
     , COUNT(x.source) total
  FROM users u
  LEFT
  JOIN 
     ( SELECT 'rented' source, user_id FROM movies_rented
       UNION ALL
       SELECT 'purchased', user_id FROM movies_purchased
     ) x
    ON x.user_id = u.user_id
 GROUP
    BY user_id
 ORDER 
    BY total DESC;

USER_ID     USERNAME        FIRST_NAME LAST_NAME       ISACTIVE  ISADMIN   RENTED    PURCHASED TOTAL
5           user5           user5FN    user5LN         1         0         4         2         6
2           user2           user2FN    user2LN         1         0         2         3         5
4           user4           user4FN    user4LN         1         0         2         2         4
3           user3           user3FN    user3LN         1         0         1         3         4
1           user1           user1FN    user1LN         1         0         1         2         3

http://sqlfiddle.com/#!2/8b3c8/10

答案 1 :(得分:0)

m.PURCHASE_ID和r.RENTED_ID的简单DISTINCT关键字解决了这个问题:

有效的查询是:

SELECT u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE, u.ISADMIN, COUNT(DISTINCT m.PURCHASED_ID) AS MOVIES_PURCHASED, COUNT(DISTINCT r.RENTED_ID) AS MOVIES_RENTED, 
(COUNT(DISTINCT m.PURCHASED_ID) + COUNT(DISTINCT r.RENTED_ID)) AS MAX_ACTIVITY
FROM TBL_USERS AS u 
LEFT JOIN TBL_MOVIE_PURCHASED AS m 
ON u.USER_ID = m.USER_ID 
LEFT JOIN TBL_RENTED_MOVIES AS r 
ON u.USER_ID = r.USER_ID 
GROUP BY u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE,u.ISADMIN
ORDER BY MAX_ACTIVITY DESC;

我添加了一个额外的MAX_ACTIVITY列,并按max_activity对结果进行排序。

指向创建多个表的示例的链接,对它们执行左连接,计算列的值,对它们进行排序和分组以及避免在执行连接时显示冗余数据:

http://sqlfiddle.com/#!2/7d44a2/2/0