查询结果需求四个字段如下: [一] id [二] name [三] tuijian_name [四] total
id 和 name 是本表中所有记录的原有字段
tuijianren_name 是表中 tuijianren_id 对应本表 id 得出的 name,如果 tuijianren_id 是 0 或者 tuijianren_id 没有对应的 id 记录则为 null
total 是统计出本表中每条记录的 id 出现在其他记录 tuijianren_id 的总数
最终理想的查询结果如下
我左连接又换右连接怎么写都不对
SELECT m.id, m.name, COUNT( `m`.id ) AS total, m2.name AS tuijian_name FROM member m RIGHT JOIN member m2 ON m.tuijianren_id = m2.id GROUP BY `m2`.id LIMIT 0 , 30 请问如何写这条语句满足这个需求? 请老司机赐教一下 谢谢!!!
SELECT m.*, m2.name AS tuijian_name, (SELECT COUNT(*) FROM member m3 WHERE m3.tuijianren_id = m2.id) AS total FROM member m JOIN member m2 ON (m.tuijianren_id = m2.id) LIMIT 0, 30
select m.id, m.name, m.tuijianren_id, q.c as total from member as m left outer join ( select tuijianren_id, count(*) as c from member group by tuijianren_id ) as q on q.tuijianren_id = m.id
select A.*, B.name as tuijian_name,(select count(C.id) from member C where C.tuijianren_id=A.id) as total from member A LEFT JOIN member B on A.tuijianren_id=B.id