SQL查询(我是否使用COUNT?)

时间:2016-09-14 20:01:30

标签: sql oracle

表格是:

ARTIST(artistID,LastName,FirstName,Nationality,DOB)

CUSTOMER(customerID,FirstName,LastName)

CUSTOMER_ARTIST_INT(artistID,CustomerID)

需要帮助为此问题进行SQL查询:

列出对所有美国艺术家感兴趣的客户的姓名。

非常新的SQL会感谢有人把我推向了正确的方向。

这是我到目前为止所不知道的,不知道我是否走在正确的轨道上。

SELECT C.FirstName, C.LastName, A.NATIONALITY
FROM dtoohey.CUSTOMER C, dtoohey.ARTIST A, dtoohey.CUSTOMER_ARTIST_INT I
WHERE C.CUSTOMERID=I.CUSTOMERID AND A.ARTISTID=I.ARTISTID;

编辑:使用oracle SQL开发人员

3 个答案:

答案 0 :(得分:0)

您希望将所有三个表连接在一起,然后指定“美国'作为WHERE条款中的国籍,我相信......

SELECT C.*
FROM Customer C
INNER JOIN customer_artist_int ca ON c.customerID = ca.customerID
INNER JOIN artist a ON a.artistID = ca.artistID
WHERE a.Nationality = 'United States'

编辑:我现在注意到您说要列出对来自美国的所有艺术家感兴趣的客户。您是说您只想要对每一位美国艺术家感兴趣的客户?如果,如果约翰史密斯只对一个美国艺术家感兴趣,他的名字应包括在内?这个逻辑会更加深入,但我会等待你的指示继续下去。

答案 1 :(得分:0)

如果您与客户一起与美国和集团的艺术家一起加入客户,您可以使用select c.FirstName, c.LastName from customer c join customer_artist_int cai on cai.CustomerID = c.customerID join artist a on a.artistID = cai.artistID and a.Nationality = 'United States' group by c.customerID, c.FirstName, c.LastName having count(*) = (select count(*) from artist where Nationality = 'United States') 子句将计数与美国艺术家的总数进行比较。如果计数匹配,则返回客户行:

class Functor g => Distributive g where
  distribute :: Functor f => f (g a) -> g (f a)
  -- other non-critical methods

答案 2 :(得分:-1)

假设国籍是一个字符串,艺术家是独一无二的:

WITH us_artists_cnt AS (
  SELECT COUNT(*) AS us_artists_cnt 
  FROM Artists
  WHERE Nationality = 'United States'),
customers_artists AS (
    SELECT C.customerID, C.FirstName, C.LastName, A.NATIONALITY, count(*) cnt
    FROM Customer C
    INNER JOIN customer_artist_int CA on CA.customerID = C.customerID
    INNER JOIN Artist A on A.artistID = CA.artistID
    WHERE A.nationality = 'United States'
    GROUP BY C.customerID)
SELECT C.customerID, C.FirstName, C.LastName, A.NATIONALITY, cnt
FROM customers_artists
WHERE cnt = us_artists_cnt

这很接近。让我知道这对你有什么用。