多个SQL选择和同一个表上的位置

I need to make a query on one table in a MYSQL database. In this query I need to make multiple selects with different where's.

My current query is:

$result = mysql_query("
    SELECT
        last_name, first_name, user_name,
        SUM(rthours) as rthours, SUM(othours) as others,
        SUM(trthours) as trthours, SUM(tothours) as tot hours
    FROM data
    WHERE labor_date BETWEEN '$start' AND '$end'
    ORDER BY last_name ASC
");

I also need to select the following two:

SUM(rthours) as vhours FROM data WHERE decription = 'vacation' AND labor_date BETWEEN '$start' AND '$end'

and

SUM(rthours) as shours FROM data WHERE decription = 'sick' AND labor_date BETWEEN '$start' AND '$end'

How can I combine all three selects into one query so that I can then use them in a table that I am outputting afterwards.

Thanks for any help!

You can use a UNION ALL. It combines the results of several queries into one result set.

Look it up: http://dev.mysql.com/doc/refman/5.0/en/union.html

You can use CASE within SUM()

SELECT last_name, first_name, user_name,
SUM( CASE WHEN decription = 'vacation' AND labor_date BETWEEN '$start' AND '$end' THEN rthours ELSE 0 END) as rthours,
SUM(CASE WHEN decription = 'sick' AND labor_date BETWEEN '$start' AND '$end' THEN rthours  ELSE 0 END) as shours,    
SUM(othours) as othours, 
SUM(trthours) as trthours, 
SUM(tothours) as tothours 
FROM data 
WHERE labor_date BETWEEN '$start' AND '$end' 
/*missing group by*/
Order by last_name ASC

For aggregate function you should use the group function

It will be a mess but you can can run those as subqueries:

SELECT last_name, first_name, user_name, SUM(rthours) as rthours,
SUM(othours) as othours, SUM(trthours) as trthours, 
SUM(tothours) as tothours, 
(SUM(rthours) as vhours FROM data WHERE decription = 'vacation' AND labor_date BETWEEN '$start' AND '$end') AS vhours
(SUM(rthours) as shours FROM data WHERE decription = 'sick' AND labor_date BETWEEN '$start' AND '$end') AS shours
FROM data WHERE labor_date BETWEEN '$start' AND '$end' Order by last_name ASC

I can't run these since I dont have a table or data but its the basic idea that matters

Got it.

This works:

$result = mysql_query("SELECT last_name, first_name,
SUM(CASE WHEN description = 'Vacation' THEN rthours ELSE 0 END) as vhours,
SUM(CASE WHEN description = 'Sick' THEN rthours ELSE 0 END) as shours,   
SUM(rthours) as rthours,
SUM(othours) as othours,
SUM(trthours) as trthours, 
SUM(tothours) as tothours
FROM data 
WHERE labor_date BETWEEN '$start' AND '$end' Group by last_name Order by last_name ASC");

Thanks for the help!!!!