MySQL LEFT JOIN Returns duplicate results Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of MySQL LEFT JOIN Returns duplicate results without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m running a query to find users that have received services from our organization, but I don’t want their user id to be duplicated. For instance, I may have user id 1 in the system 5 times, but I only want their id to count as 1 occurrence. Here’s my query:

mysql_query("SELECT COUNT(*) AS 'total'
FROM services
LEFT JOIN users
ON (services.USR_id = users.USR_id)
LEFT JOIN service_type
ON (services.SRV_type = service_type.TYPE_id)
WHERE services.MGR_orgid = $mgrorgid GROUP BY services.USR_id");

Answer

Remove the group by ..., and change the count(*) to count(distinct usr_id).

We are here to answer your question about MySQL LEFT JOIN Returns duplicate results - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji