If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with simple SQL query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-18-07, 05:20
uraknai uraknai is offline
Registered User
 
Join Date: Mar 2006
Posts: 41
Help with simple SQL query

Hi,

I'm making a simple online booking system using mysql and PHP where people can book out equipment.

There is a limited number of bookable items (10 camcorders, 20 laptops etc). When someone books an item, some of the data stored is the item id (which references the equipment table) and the number booked.
After a simple query for the equipemnt booked an a certain day at a certain time, the table looks like this:

Code:
item_id        number_booked
1                      4
4                      10
4                      5
2                      6
1                      6
4                      8
What I need is a table that shows DISTINCT item_id's and the SUM of the number booked for each of these items. So the resulting table would be:

Code:
item_id          total
1                   12
4                   23
2                   6
Hope I've explained it clearly. Could someone give me a hand?

Cheers
Reply With Quote
  #2 (permalink)  
Old 07-18-07, 06:43
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
select item_id, sum( number_booker )
from your_table
where ...
group by item_id

The where clause would contain the bit that limits things to a certain day and time etc. The important new bits are the sum() and the group by.

Mike
Reply With Quote
  #3 (permalink)  
Old 07-18-07, 07:49
uraknai uraknai is offline
Registered User
 
Join Date: Mar 2006
Posts: 41
Quote:
Originally Posted by mike_bike_kite
select item_id, sum( number_booker )
from your_table
where ...
group by item_id

The where clause would contain the bit that limits things to a certain day and time etc. The important new bits are the sum() and the group by.

Mike
Thats great. Works like a charm.

Cheers
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On