基于SQL中的COUNT从表中进行SELECT

时间:2013-10-21 08:46:27

标签: sql ms-access count group-by having

我有四张桌子:

  PAINTING            GALLERY             ARTIST              PAINTED
- PAINTING_TITLE    - GALLERY_ID        - ARTIST_ID         - PAINTED_CODE
- PAINTING_ID       - GALLERY_NAME      - ARTIST_LAST       - ARTIST_ID
                                        - ARTIST_FIRST      - PAINTING_ID

PAINTED表跟踪每位艺术家绘制的绘画。有些画作是由不止一位艺术家绘制的。我想回复两位艺术家画的画作清单。

SELECT
    PAINTING.PAINTING_TITLE AS TITLE, 
    GALLERY.GALLERY_NAME AS GALLERY
FROM
    PAINTING,
    GALLERY,
    PAINTED
WHERE
        PAINTING.GALLERY_ID = GALLERY.GALLERY_ID
    AND
        PAINTING.PAINTING_ID = PAINTED.PAINTING_ID
GROUP BY
    PAINTING.PAINTING_TITLE,
    GALLERY.GALLERY_NAME
HAVING
    COUNT(PAINTED.ARTIST_ID) = 2

这样可行,但结果中没有包含艺术家的名字。我需要在图库名称和艺术品名称中列出每个艺术家的名字,每个名称都会出现两次,但有一次会有不同的艺术家。

我正在使用Access SQL。

1 个答案:

答案 0 :(得分:0)

使用此:

SELECT
    PAINTING.PAINTING_TITLE AS TITLE, 
    GALLERY.GALLERY_NAME AS GALLERY,
    ARTIST.ARTIST_FIRST & " " & ARTIST.ARTIST_LAST
FROM
    PAINTING,
    GALLERY,
    PAINTED,
    ARTIST,
WHERE
        PAINTING.GALLERY_ID = GALLERY.GALLERY_ID
    AND
        PAINTING.PAINTING_ID = PAINTED.PAINTING_ID
    AND
        ARTIST.ARTIST_ID = PAINTED.ARTIST_ID
GROUP BY
    PAINTING.PAINTING_TITLE,
    GALLERY.GALLERY_NAME
HAVING
    COUNT(PAINTED.ARTIST_ID) = 2