Question

I'm making a database which simulates a store. I've got a table named ProductsTbl that resembles all products available for purchase, and a column named ProductType that identifies the item as a Game or Console. I want to count how many Games fit for each Console. Games have a field called Platform, which is the Console the Game runs on.

SELECT
ProductsTbl.ProductID,
ProductsTbl.ProductName,
ProductsTbl.Manufacturer,
count(ProductsTbl.ProductName = ProductsTbl.Platform)
FROM productstbl
WHERE ProductsTbl.ProductType = 'Console';

This was the only lead I had so far..

Was it helpful?

Solution

You'll need to join the table with itself to get the game list. If (as it looks) the thing that connects the game to the console is that the game's platform is set to the productname of the console, this should do it;

SELECT p1.productid, p1.productname, p1.manufacturer, COUNT(p2.productid) games
FROM productstbl p1
LEFT JOIN productstbl p2 
  ON p2.producttype = 'game' AND p2.platform = p1.productname
WHERE p1.producttype = 'Console'
GROUP BY p1.productid, p1.productname, p1.manufacturer;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top