需要帮助来编写有关此数据库的查询

时间:2019-09-06 13:08:55

标签: sql phpmyadmin

我有这个数据库,我需要有关此查询的帮助

  

查找客户ID,名字,姓氏和电影名称   当天购买机票最多的客户中的

img

我找到了当天买票最多的顾客,现在我需要查找他购买票的电影

SELECT c.*, COUNT(*) 'bought'
FROM customer c JOIN ticket t ON c.customerId=t.customerId
GROUP BY c.customerId
HAVING bought=(SELECT MAX(T1.CNT)
                    FROM (SELECT COUNT(*) AS CNT 
                          FROM customer c JOIN ticket t ON c.customerId=t.customerId
                          GROUP BY c.customerId) AS T1)

1 个答案:

答案 0 :(得分:0)

因此,如果不构建数据库并用虚拟数据填充它,我将无法测试,但是我想我为您找到了解决方案。

     SELECT C.CUSTOMERID, C.FIRSTNAME, C.LASTNAME, M.TITLE 
    FROM CUSTOMER C JOIN TICKET T ON C.CUSTOMERID = T.CUSTOMERID JOIN SHOWS S ON T.SHOWNUMBER = S.SHOWNUMBER JOIN MOVIE M ON M.MOVIEID = S.MOVIEID 
    WHERE C.CUSTOMERID IN(
SELECT C1.CUSTOMERID FROM(
SELECT CUST.CUSTOMERID, COUNT(*) 'BOUGHT' 
FROM CUSTOMER CUST JOIN TICKET TICK ON CUST.CUSTOMERID = TICK.CUSTOMERID 
GROUP BY CUST.CUSTOMERID 
HAVING BOUGHT = (SELECT MAX(T1.CNT) FROM (SELECT COUNT(*) AS CNT FROM CUSTOMER CUSTO JOIN TICKET TICKE ON CUSTO.CUSTOMERID = TICKE.CUSTOMERID GROUP BY CUSTO.CUSTOMERID)AS T1) AS C1);