optional where clause condition in mysql

rupinder18

I am working on a project where I load a list of students depending on the date selected and the teacher chosen from a dropdown.The list contains those students data whose class teacher is the teacher selected. Now i am using the following query:

select s.studentcode,s.studentname,a.attdate,if(a.period='01' or 
a.period='AM',a.status,'') as attAM,if(a.period='PM',a.status,'') as attPM 
from tblstudent s left join tblattendance a on s.studentcode=a.studentcode 
where s.classcode='10002' and a.attdate='2014-04-11'

Now the problem with the above is query is that if i chose some date like 2014-04-15 then as attendance is not marked for this date then no record is found,but i want that the student list is always displayed and if attendance is marked for that date it should be displayed accordingly else those fields will be blank

enter image description here

The above screenshot is displaying the list which I want.Now the field AM should be blank if attendance for that date is not marked and if it is marked it should contain the corresponding attendance

M Khalid Junaid

Move your AND a.attdate = '2014-04-11' to join's ON() clause so if date is not present it still returns the records for s.classcode = '10002' and a.attdate column will be null in the result set,where filter is applied to whole resultset where as using additional condition in join will filter the records from right table i.e tblattendance not on whole resultset

SELECT 
  s.studentcode,
  s.studentname,
  a.attdate,
  IF(
    a.period = '01' 
    OR a.period = 'AM',
    a.status,
    ''
  ) AS attAM,
  IF(a.period = 'PM', a.status, '') AS attPM 
FROM
  tblstudent s 
  LEFT JOIN tblattendance a 
    ON (s.studentcode = a.studentcode AND a.attdate = '2014-04-11')
WHERE s.classcode = '10002' 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Make an optional Where Clause in mysql

From Dev

Right way to use optional condition in WHERE clause

From Dev

ignoring condition in where clause in mysql

From Dev

mysql query apply condition to where clause only if certain condition satisfied

From Dev

MySQL : How to get latest row on optional where clause using group by?

From Dev

Using "If" condition in where clause

From Dev

Condition in WHERE clause (Oracle)

From Dev

SQL : If condition in where clause

From Dev

select Condition in where clause

From Dev

MySQL UNION where clause condition from the first select

From Dev

PHP MySQL PDO TextArea Where clause with condition checks

From Dev

MYSQL: How to use the result of a grouped SUM() as condition in WHERE clause?

From Dev

optional where clause jasper reports

From Dev

Sequelize optional where clause parameters?

From Dev

optional where clause jasper reports

From Dev

INNER JOIN condition in WHERE clause or ON clause?

From Dev

SQL Query: difference in condition in On clause and where clause

From Dev

Use Condition In Where Clause In Linq

From Dev

LINQ: IF condition in WHERE clause not working

From Dev

MongoDB multiple condition in WHERE clause

From Dev

SQL Server : IF Condition in WHERE clause

From Dev

Variable condition in LINQ where clause

From Dev

SQL WHERE Clause condition evaluation

From Dev

AND condition in WHERE clause on multiple columns

From Dev

IF ELSE CONDITION IN SQL WHERE CLAUSE

From Dev

Use Condition In Where Clause In Linq

From Dev

MongoDB multiple condition in WHERE clause

From Dev

Checking condition in IF vs WHERE clause

From Dev

LINQ: IF condition in WHERE clause not working

Related Related

HotTag

Archive