------解决方案--------------------
SELECT userid,NAME,COUNT(*) FROM ( SELECT a.*,b.name FROM ttg1a1 a LEFT JOIN ttg1b1 b ON MID(a.att,3,1)=b.id WHERE cls=1 UNION ALL SELECT a.*,b.name FROM ttg1a1 a LEFT JOIN ttg1c1 b ON MID(a.att,3,1)=b.id WHERE cls=2) a1 GROUP BY userid,NAME UNION ALL SELECT userid,'合计',COUNT(*) FROM ttg1a1 GROUP BY userid,'';