What is the difference between the 3 queries I have here?
The first version works on my localhost xampp mysql database (10.1.9-MariaDB). However it retrieves 0 results with my online test server mysql database (5.7.9-log MySQL Community Server GPL).
The 2nd version retrieves the expected results with both servers. But on my localhost query 2 retrieves the same result set albeit in a different order, but i'm guessing that's because I don't have an order by.
Is this an issue with the different version of mysql? and how, if at all are my queries different?
Query 1
SELECT
s.eid, e.fname, e.lname, s.id, s.starttime, s.finishtime
FROM
employees AS e
RIGHT JOIN
scheduling_shifts AS s USING (eid)
GROUP BY s.id
HAVING COUNT(s.date = '2016-01-15' OR NULL) > 0
AND COUNT(s.eid = '1' OR NULL) = 0;
Query2
SELECT
s.eid, e.fname, e.lname, s.id, s.starttime, s.finishtime
FROM
employees AS e
RIGHT JOIN
scheduling_shifts AS s USING (eid)
WHERE
s.date = '2016-01-15'
GROUP BY s.id
HAVING COUNT(s.eid = '1' OR NULL) = 0;
EDIT:: Query3
This query also retrieves the correct result set minus the NULL s.eid
's
SELECT
s.eid, e.fname, e.lname, s.id, s.starttime, s.finishtime
FROM
employees AS e
RIGHT JOIN
scheduling_shifts AS s USING (eid)
WHERE
s.date = '2016-01-15' AND s.eid <> '1'
EDIT:: Here is a test case specific to my own query on SQLfiddle
The objective is to fetch a list of possible swaps for the selected employee on the selected date. Our selected date is the 15th Jan 2016
, and selected employee who we want to swap his shift is employee 1
. The list cannot fetch his own shift, because he cannot swap shifts with himself. But it should bring up the unassigned shifts. (shifts which exist where the eid
is NULL
). So the option is bring up a list of other employees and their shifts, OR unassigned shifts on this day which we can swap ours with.
Notice that Query 1 - does produce the correct results on this, did produce the correct result on xampp (mariadb 10), but didn't on my online test server.
Query 2 also fetches the correct result, and also did both xampp and online test server.
Query 3 fetches all the shifts that are assigned, but not the unassigned ones.
Hope this explains my question a bit better.