Php 多个SQL Select和Where在同一个表上
我需要对MYSQL数据库中的一个表进行查询。在这个查询中,我需要使用不同的where进行多个选择 我目前的查询是:Php 多个SQL Select和Where在同一个表上,php,mysql,select,where,Php,Mysql,Select,Where,我需要对MYSQL数据库中的一个表进行查询。在这个查询中,我需要使用不同的where进行多个选择 我目前的查询是: $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 ho
$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
");
我还需要选择以下两项:
SUM(rthours) as vhours FROM data WHERE decription = 'vacation' AND labor_date BETWEEN '$start' AND '$end'
及
如何将所有三个选择组合到一个查询中,以便在随后输出的表中使用它们
谢谢你的帮助 您可以使用
联合所有。它将多个查询的结果组合成一个结果集
查找:您可以在SUM()中使用CASE
对于聚合函数,您应该使用组函数,这将是一个混乱,但您可以将其作为子查询运行:
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
我无法运行这些,因为我没有表格或数据,但重要的是它的基本思想得到了它
这项工作:
$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");
谢谢你的帮助 这在使用时不返回任何记录。这应该是因为表中的数据符合select语句的要求。经过一些研究,我将上面的代码编辑为:$result=mysql\u query(“选择姓氏、姓氏、总和(说明时的大小写为“休假”,然后为rthours ELSE 0 END)作为vhours、SUM(说明时的大小写为“生病”,然后为rthours ELSE 0 END)作为应付款,SUM(othours)为othours,SUM(trthours)为trthours,SUM(TOTHHOURS)为TOTHHOURS,根据姓氏ASC,从“开始日期”到“结束日期”之间的数据计算得出
但它仍然不返回任何结果。@Lawrence您是否可以添加带有样本数据集的表,这样在使用时更容易识别它不返回任何记录。由于表中的数据符合select语句的要求,因此应进行修改。
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
$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");