INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

Need some assistance cleaning up a query.

Need some assistance cleaning up a query.

(OP)
Hello,

Currently, I have a couple of queries that are taking a couple seconds to process and load the page and I was wondering if you could point me in the right direction to clean this up.

I have two tables that are being accessed over 2 queries right now.

This query grabs all the beer names for one brewery

CODE


SELECT beer.beer_id,beer.beer_name
FROM beer
WHERE beer.brewery_id='some_id'
ORDER BY beer.beer

Then I take those results and loop through them to also display some stats about the beer.  (number reviews, average score etc.)

CODE

foreach($result do this){
   SELECT AVG(total) AS score, COUNT(beer_id) AS review_count   ,MAX(total) AS high_score ,MIN(tot) AS low_score
   FROM `ratings`
   WHERE beer_id='".$row->beer_id."'
}

I was trying to use a JOIN like so, but as soon as I do, it limits the results to one row.

CODE


SELECT beer.beer_id,beer.beer_name,AVG(total)
FROM beer
JOIN ratings ON ratings.beer_id=beer.beer_id
WHERE beer.brewery_id='some_id'
ORDER BY beer.beer

Is it possible to do what I am trying to do with one query?  I assume it will cut my page load time down significantly if I can consolidate it.

Thank you
 

RE: Need some assistance cleaning up a query.

(OP)
I just noticed that when typing this to clean it up, ORDER BY in both queries should be ORDER BY beer.beer_name

RE: Need some assistance cleaning up a query.

Hi

What is your goal ? If you want to get a list of all bears together with each bear's rating statistics, then you should use group by on beer.brewery_id and beer.beer_name and no where condition.
 

Feherke.
http://free.rootshell.be/~feherke/

RE: Need some assistance cleaning up a query.

CODE

SELECT beer.beer_id
     , beer.beer_name
     , AVG(ratings.total) AS score
     , COUNT(*) AS review_count   
     , MAX(ratings.total) AS high_score
     , MIN(ratings.total) AS low_score    
  FROM beer
INNER
  JOIN ratings    
    ON ratings.beer_id = beer.beer_id
 WHERE beer.brewery_id = some_id
GROUP
    BY beer.beer_id
ORDER
    BY beer.beer_name

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: Need some assistance cleaning up a query.

(OP)
Thanks for the replies.  r937's query gets me most of the way there, it's not displaying the beer names for the beers without reviews on them, but I will be able to tweak this query to include those when I have a couple minutes.

Thank you, I didn't think to group the results, my understanding of it was a little off it appears.

Thanks again!

RE: Need some assistance cleaning up a query.

Quote:

it's not displaying the beer names for the beers without reviews on them
use LEFT OUTER JOIN instead, and change COUNT(*) to COUNT(ratings.beer_id)

smile

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: Need some assistance cleaning up a query.

(OP)
Awesome, that does it, thanks again!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close