您好,欢迎访问一九零五行业门户网

Mysql join 多张表时查询结果出了问题

因为公司产品的需求,现在要去实现一个功能,表a放的用户的基本信息,表b存的a表中用户的一些行为,表c和表b的性质一样。要求查询的时候可以按照b表或者c表中的count结果进行排序,于是就想到了join,可是出现了问题。
先贴出3张表的数据结构
create table `a` ( `id` int(11) not null auto_increment, `username` varchar(255) default null, primary key (`id`),) engine=myisam auto_increment=1;
create table `b` ( `id` int(11) not null auto_increment, `userid` int(11) default null, `dosomething` varchar(255) default null, primary key (`id`), key `userid` using btree (`userid`)) engine=myisam auto_increment=1;
create table `c` ( `id` int(11) not null auto_increment, `userid` int(11) default null, `dosomething` varchar(255) default null, primary key (`id`), key `userid` using btree (`userid`)) engine=myisam auto_increment=1;
自己做出尝试,发现查询结果不一样
select u.id, count(s.id) as sapply, count(uu.id) as ftotal from a as u right join b as s on u.id = s.userid right join c as uu on u.id = `uu`.`userid` group by `u`.`id` order by `ftotal` desc limit 10
数据明显有问题,分开关联看结果
select u.id, count(s.id) as sapply from a as u right join b as s on u.id = s.userid group by `u`.`id` order by `sapply` desc limit 10
select u.id, count(uu.id) as ftotal from a as u right join c as uu on u.id = uu.userid group by `u`.`id` order by `ftotal ` desc limit 10
大家帮忙看看,这问题出在哪呢 第一条sql语句也没有错误但是结果就是不对。
其它类似信息

推荐信息