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();
特别说明,代码为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';
上一篇: Java系列之——Java面试题
下一篇: 反转链表(迭代和递归)