Czy ktoś może pomóc w tym zapytaniu:
Lista wszystkich klientów z wieloma licznikami.
Jestem zupełnym nowicjuszem, więc wybacz mi, jeśli to jest łatwy problem do rozwiązania ..
Customers – minimum 20 records
Meters – min. 30 records
Meter Readings – min. 100 readings
Invoices – 1 per Meter Reading
Próbowałem następujące kwerendy bez powodzenia ...
select *
from Customers
LEFT OUTER JOIN Meters ON Customers.idCustomers = Meters.Customers_idCustomers
where Customers.idCustomers = Customers.idCustomers;
Próbowałem również zapytań SELECT CASE
Dzięki!
Odpowiedzi:
1 dla odpowiedzi № 1Oto jeden sposób:
select c.*
from customers c
where c.idCustomer in (select idCustomer
from Meters
group by Customers_idCustomers
having count(*) > 1
)
W MySQL możesz wyrazić to jako sprzężenie z grupą przez:
select c.*
from customers c join
meters m
on c.idCustomer = m.Customers_idCustomer
group by c.idCustomer
having count(*) > 1
0 dla odpowiedzi nr 2
Tutaj pojawia się klauzula 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