[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MySQL query Syntax with many to many relationship...

Posted on 2009-04-13
4
Medium Priority
?
308 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 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