Like the question says is there a better way to write this:
SELECT clients_lists.*,
COUNT(clients_lists_relationships.clientid)
FROM clients_lists
LEFT JOIN clients_lists_relationships
ON clients_lists.listid = clients_lists_relationships.listid
WHERE clients_lists.parentid = 1
GROUP BY clients_lists.listid;
Not really. The query looks fine to me. You can try to use aliases for better readability:
SELECT c.*,
COUNT(r.clientid)
FROM clients_lists c
LEFT JOIN clients_lists_relationships r
ON c.listid = r.listid
WHERE c.parentid = 1
GROUP BY c.listid;
But it won't really make a technical difference (not that I know).
Also make sure that both clients_lists.listid
and clients_lists_relationships.listid
are of the same datatype and length and are indexed. This will definitely help your query's performance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.