Jump to content

Need Help With mySQL Query


jamesbrauman

Recommended Posts

Hello all, how are you going?

I have a table inside my database called 'literatureworks'. This table contains information about lots of different literature works. This table is set out like so:

 

[id]  [name]  [category]  [genre]  [date_submitted] ....

 

I am using PHP and MySQL, and I want to craft a query to return the number of items per genre where the category is equal to the category I specify, as well as the current genre name and category name. I will then loop through the rows retured (hopefully there is one row per genre returned), and echo this to my page. My brain is half-dead this morning, so forgive me, but this is what I have so far:

 

//Query the database and get the result

$result = mysql_query("SELECT * FROM literatureworks, COUNT(name) WHERE category='Fiction' GROUP BY genre");

//Start our loop

while ($ow = mysql_fetch_array($result)) {

$currentcategory = "Fiction";

$currentgenre = $row['genre'];

$totalitemsingenre = $row['COUNT(name)'];

echo "$currentcategory: $currentgenre contains $totalitemsingenre items.";

}

 

I have no idea where to go from here, thankyou for reading.

Link to comment
Share on other sites

SELECT COUNT(name) as name_count FROM literatureworks WHERE category = 'Fiction' GROUP BY genre

 

It's going to be slow as hell with the WHERE and GROUP BY clause if you have a lot of data.

Unless you have a covering index.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.