ali madadi
ali madadi

Reputation: 3

how to select with two date condition

I have a date '$user_input_date' that i receive it from user and i have a query inside other query in select clause and i want to select total hours in this sub query with two condition like bellow

$sql="SELECT teacher.teacher_id,name,lname,father_name,salary_per_hour,monthly_salary,"
            . "(select ($user_month - month(salary_start_date)) from teacher where teacher_id=$id) as deff_month ,"
            . "(select ADDDATE(salary_start_date, interval deff_month month) from teacher where teacher_id=$id) as start_interval, "
            . "(select ADDDATE(start_interval, interval 1 month)) as end_interval ,"
            . " (select DATE_SUB(start_interval, interval 1 month)) as reverse_end_interval,"
            . "(**select sum(hours) from teacher_hours_late where date>= start_interval AND date<end_interval OR date>=start_interval AND date<reverse_end_interval and teacher_id=$id ) as total_late**"
            . " from teacher where teacher.branch_id=$branch_id and teacher.teacher_id=$id"

my object is to achive total hours in date interval by the date that i receive from user like that if start_interval>$user_input_date then i want to select interval between start_interval and end_interval otherwise select interval between start_interval and reverce_end_interval in where clause of below query my candition in where clause is wrong because i have no idea please help me dears select sum(hours) from teacher_hours_late where date>= start_interval AND date<end_interval OR date>=start_interval AND date<reverse_end_interval and teacher_id=$id ) as total_late

Upvotes: 0

Views: 585

Answers (1)

WorkSmarter
WorkSmarter

Reputation: 3808

Parenthesis are required to isolated the between date conditionals.

Before

where date>= start_interval AND date<end_interval OR date>=start_interval AND date<reverse_end_interval

After

where (date>= start_interval AND date<end_interval) OR (date>=start_interval AND date<reverse_end_interval)

Upvotes: 2

Related Questions