$mysqlquery= mysql_query("
SELECT COUNT(patient_id) AS idpateint,
patient_id
FROM patient
WHERE STR_TO_DATE(date_enter,'%d/%m/%Y' )
BETWEEN STR_TO_DATE( '$repeat','%d/%m/%Y' ) AND STR_TO_DATE('$to','%d/%m/%Y')
AND patient_type='opd'
AND patient_id ='$idpatient1'
ORDER BY STR_TO_DATE(date_enter,'%d/%m/%Y' )
");
实际上我在db中以dd / mm / yyyy格式保存了日期。订单不起作用。没有sql错误,但日期不按顺序排列。任何人都可以帮我解决问题
答案 0 :(得分:2)
以下是您的查询:
select count(patient_id) as numpatients, patient_id
from patient
where STR_TO_DATE(date_enter, '%d/%m/%Y' ) between STR_TO_DATE('$repeat', '%d/%m/%Y' ) and
STR_TO_DATE('$to', '%d/%m/%Y') and
patient_type = 'opd' and
patient_id = '$idpatient1'
order by STR_TO_DATE(date_enter, '%d/%m/%Y' )
count()
中有select
。这会将查询转换为聚合查询,因此它只返回一行。此外,您只选择一个患者身份证。我可以想象你想要按日期计算,因为你专注于date
。
以下将按日期计算,无论患者如何:
select STR_TO_DATE(date_enter, '%d/%m/%Y' ), count(patient_id) as numpatients
from patient
where STR_TO_DATE(date_enter, '%d/%m/%Y' ) between STR_TO_DATE('$repeat', '%d/%m/%Y' ) and
STR_TO_DATE('$to', '%d/%m/%Y') and
patient_type = 'opd' and
group by STR_TO_DATE(date_enter, '%d/%m/%Y' )
order by STR_TO_DATE(date_enter, '%d/%m/%Y' );
顺便说一下。不要认为查询对STR_TO_DATE()
的所有调用看起来都很丑陋。除了使查询效率降低之外,它们还很难看。使用数据库数据类型在数据库中存储日期。这就是他们的目的。