How to solve the problem of data loss in multi-table join table query?

use leftJoin multi-table join lookup. Finally, a field in select is a field from count in one of the right tables, but if there is no count data in that table, the entire query will lose the result. Now you want to keep the data in the left table if there are no results in the query.

the code looks like this:

SELECT a.name,COUNT(b.id) AS com FROM `a` LEFT JOIN `b` ON `a`.`uid` = `b`.`uid` LEFT JOIN `c` ON `d`.`id` = `c`.`id`  ORDER BY `a`.`id` DESC LIMIT 10 OFFSET 0

finally, if there is no corresponding data in b, the query data will be lost

Nov.25,2021

try adding a gourp by a.name in front of order by

Menu