0

我有四张桌子:

  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, 
   ARTIST.ARTIST_FIRST AS ARTISTFIRST, 
   ARTIST.ARTIST_LAST AS ARTISTLAST
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
AND (SELECT COUNT(*) FROM PAINTED, PAINTING, ARTIST
WHERE PAINTING.GALLERY_ID = GALLERY.GALLERY_ID
AND PAINTING.PAINTING_ID = PAINTED.PAINTING_ID
AND ARTIST.ARTIST_ID = PAINTED.ARTIST_ID) = 2;

这不会返回我需要的结果。你对如何只选择有两位艺术家的画作有什么想法吗?

我正在使用 Access SQL,任何帮助将不胜感激。

4

1 回答 1

0

使用GROUP BY代替内部SELECT

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
于 2013-10-21T01:51:23.620 回答