[SOLVED] Trying to limit an outer join to return just 1 result per customer |
6/2/2021 12:26:36 AM |
PHPRunner General questions | |
D
DealerModules authorDevClub member
I have a MYSQL statement that returns a list of customers that have a status = "Installation". I have a left outer join that pulls the contacts from another table "contacts". My question is that I would like to only pull 1 contact per customer in my list. My problem is that if I have more than one contact per customer then I get multiple lines in my list per customer. To limit the list, I have tried using LIMIT 1 but my result is just one customer only. I need all the customers but just LIMIT one contact per customer. Below is my mysql select statement. Any direction appreciated. SELECT |
|
![]() |
Admin 6/2/2021 |
It should be possible. Check this post for inspiration. The key is not to join with the second table directly but to do a subquery with GROUP BY and a function like MAX() that would only return a single record from the joined table. |
![]() |
Dalkeith 6/2/2021 |
Yes I have done this with SQL in SQL Server before.. MySQL solution looks like it will be similar to this. |
D
|
DealerModules authorDevClub member 6/2/2021 |
Thanks for the direction. |