[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

MySQL query Syntax with many to many relationship...

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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

656 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