2016-12-13 3 views
1

So holen Sie Daten von Kontakten, die 2 Produkte haben?Fetch Kontakte mit Projekten mehr als 2 im Berichtsmodul

Kontakte Tabelle

id name 
1 Jon Doe 

Projekte Tabelle

id Project_name 
1 prod_a 
2 prod_b 

project_contacts Tisch

id contact_id project_id 
1  1   1 
2  1   2 

Ich will Ergebnisse dieser Kontakte, die sowohl prod_a und prod_b haben

Wie dies in Berichtsmodul zur Abfrage

select c.name 
from 
Contacts c, 
Projects p, 
project_contacts pc 
WHERE 
c.id = pc.contact_id 
and p.id = pc.project_id 
and p.name = "prod_a" 
and p.name = "prod_b" 

I Ergebnis

geholt
name 
-------- 
John Doe 
John Doe 

Antwort

0

Sie benötigen werden möchte, ein JOIN unter-st der Tabellen durchzuführen und dann group by wie

select c.name 
from Contacts c 
join project_contacts pc on c.id = pc.contact_id 
join Projects p on o.id = pc.project_id 
WHERE p.Project_name in ('prod_a','prod_b') 
group by c.id 
having count(distinct c.name) = 2; 
+0

Entschuldigung für die verspätete Antwort kann ich das Ergebnis ohne Verwendung der Anzahl erreichen. –

+0

@NarayanPadhi, leider nein, 'count()' hier ist, um sicherzustellen, dass Sie beide das Produkt – Rahul

Verwandte Themen