我有两个表:“系列”和“产品”
“系列”包含系列书籍的名称,“产品”包含个别书籍名称的详细信息。
所以是这样的:
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?)
任何帮助都将不胜感激。提前致谢!