Skip to main content
December 16, 2009
Question

Loop DB for multiple values

  • December 16, 2009
  • 1 reply
  • 273 views

I wanted to get an opinion on the best way to go about doing this.  Here's what I need to accomplish:

I want to query my db for a range of values. (find motors between 5 and 50 HP)  Then, from that query, I want to be able to count items that have the same HP so that my page can say we have x number of 5 hp motors available, x number of 50 HP motors available.  The curve ball is that while there is a standard for the motors, there is always a few odd motors so I can't create static queries  of certain HPs.  Does that make sense?

I've attached the layout of the output if it helps. (The values are only there to fill out the look and aren't accurate)  I also don't need the code, I just want to know the best process.  I have an idea of how to do it but I want to make sure it's the best way.  Thanks for the help!

This topic has been closed for replies.

1 reply

Participating Frequently
December 16, 2009

To count the number of items within a group, use the group by clause.

Select count(*), hp from mytable

where hp between 5 and 50

group by hp

December 16, 2009

Wow, thank you!  I was thinking I was going to have to go through the database a second time.  I didn't know there was a simple SQL key word.  Thanks so much for the help, I'm glad I asked!