MySQL选择多个表并选择日期并与今天的日期进行比较

时间:2018-03-05 02:14:11

标签: php mysql mysqli

我正在尝试在数据库中选择日期并将其与今天的日期进行比较。我当前的代码没有错误,但它没有给我任何结果。

我的代码:

$query = mysqli_query($mysqli, "select *, date_format(date, '%Y-%m-%d') from appointment
                                LEFT JOIN doctor
                                ON doctor.doctor_id = appointment.appointment_title
                                LEFT JOIN patient
                                ON patient.patient_id = appointment.patient_id
                                where appointment.appointment_title = $session_id AND
                                date(appointment.date) = CURDATE()
                                order by appointment.date ASC");

编辑:

以下是appointment.date = 03/05/2018的示例,数据类型为var

2 个答案:

答案 0 :(得分:0)

使用STR_TO_DATE()功能根据当前日期格式转换日期

$query = mysqli_query($mysqli, "SELECT *, STR_TO_DATE(date, '%d/%m/%Y') FROM appointment
                            LEFT JOIN doctor
                            ON doctor.doctor_id = appointment.appointment_title
                            LEFT JOIN patient
                            ON patient.patient_id = appointment.patient_id
                            WHERE appointment.appointment_title = $session_id AND
                            date(STR_TO_DATE(appointment.date, '%d/%m/%Y')) = CURDATE()
                            ORDER BY appointment.date ASC");

答案 1 :(得分:-1)

通过使用CURDATE(),您可以使用以下内容: 20180306 。 试试这个:

$query = mysqli_query($mysqli, "select *, date_format(date, '%Y-%m-%d') from appointment
                                LEFT JOIN doctor
                                ON doctor.doctor_id = appointment.appointment_title
                                LEFT JOIN patient
                                ON patient.patient_id = appointment.patient_id
                                where appointment.appointment_title = $session_id AND
                                date(appointment.date) = DATE(CURDATE())
                                order by appointment.date ASC");