2

「シリーズ」と「製品」の2つのテーブルがあります

「シリーズ」には一連の本の名前が入り、「製品」には個々の本のタイトルの詳細が入ります。

だから、このようなもの:

Series Table:
id      series_name               series_description
1       Lord of the Rings         Trilogy of fantasy books recently made into film
2       A Song of Ice and Fire    Epic series of novels currently showing on HBO
3       Harry Potter              Famous Children's book series also loved by adults

Product Table:
id      product_name              series_id     author             etc...
1       Fellowship of the Ring    1             JRR Tolkien
2       The Two Towers            1             JRR Tolkien
3       Return of the King        1             JRR Tolkien
4       A Game of Thrones         2             George R R Martin
5       A Clash of Kings          2             George R R Martin
6       A Storm of Swords         2             George R R Martin
7       A Feast for Crows         2             George R R Martin
8       A Dance with Dragons      2             George R R Martin
9       Harry Potter and the...   3             JK Rowling
etc.

SELECT * FROM series と COUNT(*) FROM product を使用して、クエリがシリーズ情報を含むテーブルを返し、各シリーズに対応するデータベース内の製品数がテーブルの最後の列として追加されるようにします。ジャンル別にもやりたいので、そこに追加の WHERE 句があります。ジャンルとして「ファンタジーと魔法」を選択すると、次のようになります。

id      series_name               series_description         number_of_products
1       Lord of the Rings         Trilogy of Fantasy...      3
2       A Song of Ice and Fire    Epic Series of Novels...   5
3       Harry Potter              Famous Children's book...  7

LEFT JOIN が必要かもしれないと思いますが、これまでの私の最善の試みはうまくいきませんでした。

これは私がこれまでに持っているものですが、おそらく完全に間違っていると思います。

SELECT  series.id,                                   
series.series_name,                                         
series.publisher_id,                                       
series.description, 
series.image, 
COUNT (product.*) as nRows 
FROM series 
LEFT OUTER JOIN product
ON series.id = product.series_id                    
WHERE series.genre = 'Fantasy and Magic'
GROUP BY ... (do I need a GROUP BY?) 

どんな助けでも本当に感謝しています。前もって感謝します!

4

2 に答える 2

7

もうすぐです。これを試して。

SELECT  series.id,                                   
series.series_name,                                         
series.publisher_id,                                       
series.description, 
series.image, 
COUNT (product.id) as nRows 
FROM series 
LEFT OUTER JOIN product
ON series.id = product.series_id                    
WHERE series.genre = 'Fantasy and Magic'
GROUP BY series.id,                                   
series.series_name,                                         
series.publisher_id,                                       
series.description, 
series.image
于 2013-02-01T09:05:30.260 に答える
0

これは役に立ちませんか。

select s.id, 
   s.series_name, 
   s.series_description, 
   (select count(*) from Products p where p.series_id = s.id) number_of_products 
from Series s
于 2013-02-01T09:08:46.297 に答える