2012-09-23 95 views
1

我有2個表。一個叫artist。下面是表的結構:mysql查詢2個表的計數

artistID lastname firstname nationality dateofbirth datedcease 

其他表稱爲work

workId title copy medium description artist ID 

什麼是SQL查詢列出任何藝術作品的詳細信息(包括誰創建的工作藝術家)有數據庫中記錄了多個副本?

回答

1

試試這個:

SELECT 
    w.copy, w.title, w.description, w.medium, 
    a.firstname + ' ' + a.lastname AS 'Artist created the work' 
FROM artists a 
INNER JOIN 
(
    SELECT * 
    FROM work 
    WHERE artistID IN 
    ( 
     SELECT artistID 
     FROM work 
     GROUP BY artistID 
     HAVING COUNT(*) > 1 
    ) 
) w ON a.artistID = w.artistID 

這裏是一個demo in SQL Fiddle