1

I have a MySQL database with a games table. I also have publishers and developers tables. Each row in the games table has publisher and developer columns, which correspond with the other two tables.

  • games.Publisher & publishers.PublisherID
  • games.Developer & developers.DeveloperID

I'm trying to count the number of games each publisher and developer has, and display these with php into two tables on separate web pages. I know I need to create a loop to populate the number of games for each publisher/developer present in the table rows, but I can't figure out how.

2
  • What about to try some coding and post your code when you get stuck? Most of people don't have time to create it completely for you. Commented Sep 7, 2011 at 7:50
  • Can you add your Mysql query ? Commented Sep 7, 2011 at 7:50

1 Answer 1

0

You can do a count in SQL.

SELECT p.*, count(g.publisher) as number_of_games
FROM publishers p
LEFT JOIN games g ON (g.publisher = p.publisherID)
GROUP BY p.publisher WITH ROLLUP

This will give you the count per publisher with a total added to the bottom row.
If you just want the total, drop the last time from the query.

The query is the same for developers, just replace publisher with developer

Links:
http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html
http://www.tizag.com/mysqlTutorial/

2
  • Thank you very much Johan, this worked perfectly. I have been trying to get the query right and failed miserably. I really appreciate your help! Commented Sep 7, 2011 at 17:24
  • You're welcome, feel free to accept the answer. A tip for next time, post the code that you've tried (and failed with), you'll get more answers that way; and maybe some new insights along the way. Commented Sep 7, 2011 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.