-1
$data = true ;    
$query1 = "SELECT count(attendence) as total_attendence     
           FROM employee  
           INNER JOIN attendence ON employee.emp_id = attendence.employee_id  
if($data , 'where $this->action='$this->value' AND    MONTH(date)=MONTH(CURDATE())','') ";

Above sql code is giving me error "There is an error near if statement "

wasim
  • 51
  • 6

1 Answers1

1

Right, SQL is a syntax, not a programming language, so you can't use IF statements.

"SELECT count(attendence) as total_attendence FROM employee INNER JOIN attendence ON employee.emp_id = attendence.employee_id AND MONTH(date)=MONTH(CURDATE())' , '') ";

What are you trying to accomplish here? if($data , 'where $this->action='$this->value'

Using an IF Statement in a MySQL SELECT query

The IF/THEN/ELSE construct you are using is only valid in stored procedures and functions. Your query will need to be restructured because you can't use the IF() function to control the flow of the WHERE clause like this.

The IF() function that can be used in queries is primarily meant to be used in the SELECT portion of the query for selecting different data based on certain conditions, not so much to be used in the WHERE portion of the query:

SELECT IF(JQ.COURSE_ID=0, 'Some Result If True', 'Some Result If False'), OTHER_COLUMNS FROM ... WHERE ...

Community
  • 1
  • 1
Ryan G
  • 380
  • 1
  • 11
  • IF(expr,if_true_expr,if_false_expr) MSDN say you can use if() , if -- else -- . case condition in sql queries – wasim Mar 12 '16 at 06:19
  • i want to run two queries into one , when employee id is true "where condition should execute " otherwise query should fetch for all employee – wasim Mar 12 '16 at 06:23
  • The "L" in SQL stands for "language". SQL absolutely *is* a language. It *has* a syntax, but it *is* a language. – Bohemian Mar 12 '16 at 08:20