无法提出回答此查询的代码。

时间:2017-12-25 17:18:32

标签: mysql sql sql-server database join

SQL> select * from displayed_on;

    ID NAME
     2 History of Art
     4 History of Art
     1 Berserk Charity
     5 Berserk Charity

SQL>从Art_Object中选择*;

    ID YEAR                      TITLE                     DES                       A_NAME
     1 1890                      Old Man                   An old man in the dark    van Gogh
     2 1894                      Cat                       White cat in black canvas van Gogh
     3 1853                      Monalisa                  Smiling woman             Leonardo
     4 1888                      The meeting               Two men talking           Picaso
     5 2017                      The crimson stone         Group of characters       Omar

SQL>

他们问我的问题是(显示现在每位艺术家展出的艺术品的数量。)展出意味着他们在" displayed_on"表

2 个答案:

答案 0 :(得分:2)

只有JOIN两个表with group by和count:

SELECT
  o1.A_Name,
  COUNT(COALESCE(o2.ID, 0)) AS TotalNOfObjects
FROM Art_Object as o1
LEFT JOIN displayed_on AS o2 ON o1.ID = o2.ID
GROUP BY o1.A_Name;

<强> Results

|   A_Name | TotalNOfObjects |
|----------|-----------------|
| Leonardo |               1 |
|     Omar |               1 |
|   Picaso |               1 |
| van Gogh |               2 |

答案 1 :(得分:0)

答案是:

select count(ID), A_NAME 
from Art_Object 
where ID in (select ID from displayed_on) group by (A_NAME);