MySQL,苦苦于查询

时间:2017-03-23 03:05:16

标签: mysql

这是我当前的查询

SELECT sub.Name, sub.playlistid
FROM (SELECT TrackId, playlistId, track.name AS Name
  FROM playlisttrack NATURAL JOIN track
ORDER BY TrackId) AS sub

返回以下内容。

Name   PlaylistID
For Those About To Rock (We Salute You) 8
For Those About To Rock (We Salute You) 17
For Those About To Rock (We Salute You) 1
Balls to the Wall   1
Balls to the Wall   8
Balls to the Wall   17
Fast As a Shark 8
Fast As a Shark 1
Fast As a Shark 17
Fast As a Shark 5

我无法弄清楚如何进行计数,因此会做类似

的事情
Name    NumberofPlaylists
For Those About To Rock (We Salute You) 3
Balls to the Wall   3
Fast as a Shark 4

无论我做什么计数,它只是给了我

For Those About To Rock (We Salute You) 8715 (total amount of tuples)\

类似问题

我能够做到这一点。

SELECT sub.BillingCountry, sub.aid, sub.title
  FROM(SELECT BillingCountry, Title, album.AlbumId AS aid#Count oftime purchased in that country
    From album natural join track Natural join invoiceline NATURAL JOIN invoice
    ORDER BY AlbumId) as sub
Order BY sub.aid, sub.billingcountry

这就是我得到的

Brazil  1   For Those About To Rock We Salute You
Canada  1   For Those About To Rock We Salute You
Canada  1   For Those About To Rock We Salute You
Italy   1   For Those About To Rock We Salute You
Italy   1   For Those About To Rock We Salute You
Italy   1   For Those About To Rock We Salute You
Norway  1   For Those About To Rock We Salute You
Norway  1   For Those About To Rock We Salute You
Norway  1   For Those About To Rock We Salute You
Norway  1   For Those About To Rock We Salute You
Canada  2   Balls to the Wall
Germany 2   Balls to the Wall

但是,现在我想计算一个国家购买东西的时间,所以我想要的东西是

Norway 4 For those about to rock we salute you
...

这是否相似?

select distinct a.country,b.playlist,a.cnt from
(select country,count(1) as cnt  from yourtable group by country) as a

left join

(select * from  (SELECT sub.BillingCountry, sub.aid, sub.title
                    FROM(SELECT BillingCountry, Title, album.AlbumId AS aid
                        From album natural join track Natural join invoiceline NATURAL JOIN invoice
                        Order BY sub.aid, sub.billingcountry)) as b 
on a.country = b.country

2 个答案:

答案 0 :(得分:0)

我用你的例子试试这个:

select a.BillingCountry,cnt, b.title from
(select a.BillingCountry,count(1) as cnt from
(SELECT sub.BillingCountry, sub.aid, sub.title
  FROM(SELECT BillingCountry, Title, album.AlbumId AS aid#Count oftime purchased in that country
    From album natural 
    join track Natural 
    join invoiceline NATURAL 
    JOIN invoice
    ORDER BY AlbumId) as sub
Order BY sub.aid, sub.billingcountry) as a group by a.BillingCountry) as a

left join

(select sub.BillingCountry, sub.aid, sub.title from 
(SELECT sub.BillingCountry, sub.aid, sub.title
  FROM(SELECT BillingCountry, Title, album.AlbumId AS aid#Count oftime purchased in that country
    From album natural 
    join track Natural 
    join invoiceline NATURAL 
    JOIN invoice
    ORDER BY AlbumId) as sub
Order BY sub.aid, sub.billingcountry) as b
on a.BillingCountry = b.BillingCountry

答案 1 :(得分:0)

SELECT track.name 
    , COUNT(*)
FROM playlisttrack 
    NATURAL JOIN track
GROUP BY track.name