Solved

MySQL query Syntax with many to many relationship...

Posted on 2009-04-13
4
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …

726 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