欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql链表查询,右表数据不唯一,数据重复查询问题处理

程序员文章站 2022-04-12 13:14:24
...
$subQuery="SELECT * FROM jjrxt_hetong_split WHERE cid=3 GROUP BY uid";
$count=M('yonghu')->alias('b')->join("LEFT JOIN  ($subQuery)  a  ON a.uid=b.id")->where($map)->count();
$data=M('yonghu')->alias('b')->join("LEFT JOIN  ($subQuery) as a  ON a.uid=b.id")->where($map)->field('a.*,b.id as id,b.id as uid,b.bumen as bumen_id')->page($page,$limit)->select();

mysql链表查询,右表数据不唯一,数据重复查询问题处理

 特别说明,代码为TP3,以左表用户表id为基础表,链表查询右表且去重;需要注意的是,如果需要查询右表CID=3的数据,必须写在子查询里面不然查询不到数据。

正确写法:

 $sql='select * from jjrxt_yonghu as a left join (select * from jjrxt_hetong_split WHERE cid=3 group by uid) as b on a.id = b.uid ';

错误写法

 $sql='select * from jjrxt_yonghu as a left join (select * from jjrxt_hetong_split group by uid) as b on a.id = b.uid  WHERE a.cid=3';