I have 4 tables.
User table:
id col1 col2
CoursesAssigned table:
id user_id course_id approved
CourseInfo table:
id parent_id
CourseParents table:
id start_date end_date
I think the table names and its column names are self explanatory.
I have 2 kinds of users - i) assigned ii) unassigned. I show them in 2 different pages.
While showing the assigned students I need those students from users table for each of whom there is at least one row in CoursesAssigned table where user_id
is his own user id and the approved
field is 1 and the course_id
in that row has its own parent_id
(from CourseInfo) with end_date
(from CourseParents) greater than or equal to today.
For showing unassigned students, I need those students from users table, for each of whom -
either
there is NO row in CoursesAssigned table where the user_id
is his own user id and the approved
column has a value 1. That is for an unassigned user, there may exist a row with his own user id but the approved
field contains 0.
or
there may be rows in CoursesAssigned table with the user_id
being his own user id and the approved
field having a value 1 but the parent_id
obtained from CourseInfo has from CourseParents an end_date
which is less than today's date.
I can write a query for assigned students like:
$date=date('Y-m-d');
$records = User::join('CoursesAssigned','users.id','=','CoursesAssigned.user_id')
->join('CourseInfo','CourseInfo.id','=','CoursesAssigned.course_id')
->join('CourseParents','CourseParents.id','=',
'CourseInfo.parent_id')
->where('CoursesAssigned.approved','=',1)
->where('CourseParents.end_date','>=',$date)
->select('users.id','users.col1','users.col2')
->orderBy('users.id','desc');
But that should not produce the correct result as that does not check CoursesAssigned table for at least 1 row that meets all mentioned criteria. Q1) Or should it ?
Q2) What is about the query that fetches only the unassigned students ?
EDIT : The answer can be in ORM, query builder or even raw MySql for Laravel format.
EDIT2 : Let me clarify the scenario here : I need to fetch both assigned and unassigned users separately.
To obtain assigned users I have 1 rule: How can I get those users who have at least 1 approved course in CoursesAssigned table and the parent (obtained from CourseInfo table )of that course has the end_date (in CourseParents table) greater than or equal to today.
To obtain unassigned students I have 2 rules :
Rule 1: Get those tudents who do not have any approved course (i.e. all courses have approved =0). They are unassigned students
Rule 2: Get those students who have approved courses but none of the approved courses meet the criteria of those for assigned students . That means there is no approved course there that has a parent whose end_date is greater than or equal to today.They are also unassigned students.
CoursesAssigned
table. Each course in that table will have a parent inCourseInfo
table. Each parent (i.e. row) in this table will have its details inCourseParents
table. – UncausedHow can I get all assigned users where approved is 1
. – Colognecourse_id
inCoursesAssigned
, is that a foreign key ofCourseInfo
that maps toid
in theCourseInfo
? – Colognecourse_id
inCoursesAssigned
is a foreign key ofCourseInfo
that maps toid
in theCourseInfo
. I said in OPI think the table names and its column names are self explanatory
. However, I clarified it now. – Uncaused