Hi, I have spent hours on this one, it is bound to be something simple that I am doing wrong, so please help an old guy cross this road as a good turn!
I have to prepare a report of those pupils who have not got any "naughty points" .
This was not too difficult, as I used something like this
SELECT
nd_pupils.p_lastname
FROM
nd_pupils
Left Join nd_behaviours ON nd_behaviours.pup_id = nd_pupils.id_pup
WHERE
nd_behaviours.id_det IS NULL
This showed me all the pupils who have not got an entry in the behaviours table, as evinced by an absence of a record with an entry in the id_det field.
That works fine, but then, once again, my goal posts have been moved.
The new query has to allow for searching for any pupils who have no "naughty points" between two variable dates, so I need a search optioin for the dates.
BUT, teachers being teachers, they cannot work a calender, so I have to allow them to search for dates in the form of academic weeks. These are gleaned from another look up table in the form of
Date week
each week has 5 dates that apply to the week,
As the naughty points are awarded, the date given is recorded.
So my advanced search page has Week, between and two boxes for numbers between 1 and 40.
Strangely enough, teachers only work 40 weeks of the year. And only from 8.55 a.m. to 3.00 p.m.
So now the query looks like this
SELECT
p.id_pup,
Concat(p.p_firstname, " ", p.`Middle name(s)`, " ", p.p_lastname) AS Pupil,
p.reg AS `Tutor Group`,
w.week,
COUNT(w.week) AS Tracking
FROM nd_pupils AS p
LEFT OUTER JOIN nd_behaviours AS b ON p.id_pup = b.pup_id
LEFT OUTER JOIN weeks AS w ON b.datestamp = w.`date`
GROUP BY p.id_pup, p.reg
HAVING COUNT(w.week) < '1'
The left outer joins allow me to get all the records .
this produces the same set of results as the query above, not a bad guess was it.
BUT
when I search for the weeks between, I get a blank screen
Now I have used joins, no where clause, as well as a Having clause as I remember from years ago that the having was applied after the search was done, what is going wrong please?
Thought about this a bit more, what I need to do is first of all filter the "naughty point" records by date, so I have only those kids awarded naughty points during the period specified, THEN i need to apply the left outer join to find the kids who do NOT have naughty points awarded, then I can group them and use the output for the report.
How can I force PHPR to do this, do I need a 2 stage query somehow, create a temp table and apply the second half of the query to the temp table?
All help gratefully accepted.