-1

有人可以帮忙解决这个问题吗:

列出所有拥有多个仪表的客户。

我是一个完整的新手,如果这是一个容易解决的问题,请原谅我。

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

我尝试了以下查询但没有成功...

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

我也尝试过 SELECT CASE 查询

谢谢!

4

2 回答 2

1

这是一种方法:

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

在 MySQL 中,您还可以通过以下方式将其表示为与组的联接:

select c.*
from customers c join
     meters m
     on c.idCustomer = m.Customers_idCustomer
group by c.idCustomer
having count(*) > 1
于 2013-03-29T21:53:23.553 回答
0

这就是 GROUP BY 子句的用武之地!

select customers.*, count(*) meter_count
from customers
inner join meters on customers.idCustomer = meters.idCustomer
group by customers.idCustomer
having meter_count > 1
于 2013-03-29T21:56:44.037 回答