Solved

MySQL query Syntax with many to many relationship...

Posted on 2009-04-13
4
302 Views
Last Modified: 2012-05-06
I have a table employees and a table shifts.  They have a many to many relationship.

I want to be able to view the employees who are logged into two/three specific shifts.
So the query is kinda like this...

SELECT SHIFTS.EMP_ID,
      SHIFTS.SHIFT_ID,
      EMPLOYEES.EMP_FNAME
FROM EMPLOYEES INNER JOIN SHIFTS ON EMPLOYEES.EMP_ID = SHIFTS.EMP_ID
WHERE SHIFT_ID = (26 AND 27 AND 28)

except the where statement is clearly wrong.  I want it to return the employees who are logged onto the shifts 26 AND 27 AND 28.

Please help with the syntax.  Thanks in advance!
Nick
0
Comment
Question by:Chimeraza
  • 3
4 Comments
 
LVL 14

Expert Comment

by:racek
ID: 24128788
SELECT SHIFTS.EMP_ID,
      SHIFTS.SHIFT_ID,
      EMPLOYEES.EMP_FNAME, count(distinct SHIFT_ID) shifts
FROM EMPLOYEES
JOIN SHIFTS ON EMPLOYEES.EMP_ID = SHIFTS.EMP_ID
WHERE SHIFT_ID IN (26 ,27 ,28)
group by 1,2,3
HAVING count = 3;
0
 
LVL 14

Expert Comment

by:racek
ID: 24128791
oops

SELECT SHIFTS.EMP_ID,
      SHIFTS.SHIFT_ID,
      EMPLOYEES.EMP_FNAME, count(distinct SHIFT_ID) shifts
FROM EMPLOYEES
JOIN SHIFTS ON EMPLOYEES.EMP_ID = SHIFTS.EMP_ID
WHERE SHIFT_ID IN (26 ,27 ,28)
group by 1,2,3
HAVING shifts = 3;
0
 
LVL 14

Accepted Solution

by:
racek earned 500 total points
ID: 24128804
or more info:
SELECT SHIFTS.EMP_ID,
      SHIFTS.SHIFT_ID,
      EMPLOYEES.EMP_FNAME,
      count(distinct IF(SHIFT_ID IN (26 ,27 ,28),SHIFT_ID, NULL)) shifts,
      COUNT( distinct SHIFT_ID) total_shifts
FROM EMPLOYEES
JOIN SHIFTS ON EMPLOYEES.EMP_ID = SHIFTS.EMP_ID
group by 1,2,3
HAVING shifts = 3;

if SHIFTS has UNIQUE EMP_ID + SHIFT_ID, you can remove DISTINCT from count
0
 
LVL 3

Author Closing Comment

by:Chimeraza
ID: 31569439
Thanks, works like a charm!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question