2013-03-29 2 views
-1

일부 쿼리는 다음 쿼리를 사용하십시오.이 쿼리는 어떻게 해결합니까?

여러 미터가있는 모든 고객을 나열하십시오.

나는 ... 나는 성공없이 다음과 같은 쿼리를 시도

Customers – minimum 20 records 
Meters – min. 30 records 
Meter Readings – min. 100 readings 
Invoices – 1 per Meter Reading 

..이 해결하기 쉬운 문제가있는 경우 완전한 newb 그래서 용서

select * 
from Customers 
LEFT OUTER JOIN Meters ON Customers.idCustomers = Meters.Customers_idCustomers 
where Customers.idCustomers = Customers.idCustomers; 

I 해요 또한 SELECT CASE 쿼리를 시도했습니다

감사! MySQL의에서

select c.* 
from customers c 
where c.idCustomer in (select idCustomer 
         from Meters 
         group by Customers_idCustomers 
         having count(*) > 1 
        ) 

, 당신은 또한 의해 그룹과 가입으로 이것을 표현할 수있다 : 여기

+1

데이터 구조는 무엇입니까? –

+0

정확히 무엇을하려고합니까? 'Meters' 레코드가 둘 이상있는'Customers' 레코드를 모두 찾으십시오. 테이블은 어떻게 생겼습니까? – zajd

+0

데이터베이스의 구조에 따라 다르지만 한 가지 공통적 인 해결책은'group by '및'counting (*)> 1'이있는 내부 조인입니다. – theglauber

답변

1

하나의 방법입니다

GROUP BY 절은 어디에서 오는 여기
select c.* 
from customers c join 
    meters m 
    on c.idCustomer = m.Customers_idCustomer 
group by c.idCustomer 
having count(*) > 1 
+0

빠른 응답을 보내 주셔서 감사합니다. 결과적으로 내 문제가 있습니다. 다음 코드로 해결되었습니다. SELECT *, COUNT (*) 고객 INNER JOIN 미터 ON Customers.idCustomers = Meters.Customers_idCustomers GROUP BY Customers.idCustomers HAVING COUNT (*)> 1; – tercou1

0

입니다!

select customers.*, count(*) meter_count 
from customers 
inner join meters on customers.idCustomer = meters.idCustomer 
group by customers.idCustomer 
having meter_count > 1 
관련 문제