Web Hosting Talk







View Full Version : mySQL: Ordering by SUM from another table entry?


crEA-tEch
02-17-2007, 06:17 AM
Hi there...

I think this is a simple question, am not sure!

I've got to order a database query going by what a value totals in another table.

I'm querying the staff table to get a list of staff - Then with their staff id's, I'm querying another table to get their total sales, and total sales units.

Everything is fine but I need to order the table by the highest sales to the lowest sales... Here's my initial query...

$query_o = "SELECT * FROM staff ORDER BY figures.sum(service_price)";
(The syntax is wrong... but is it something like that?)

This is the next query...

$sql = "SELECT SUM(service_price) AS total FROM figures WHERE staff_id='$staff_id_queried' AND " . join (" AND " , $cond ) ." ";
(This syntax i know is correct)

Anybody know what I'm missing??

Thanks in advance,

Nick

gabeosx
02-17-2007, 09:39 AM
select sum(service_price) as total from staff, figures where figures.staff_id = staff.staff_id order by total