首页 > 代码库 > 3表查询,1:多:多,根据1查多再查多

3表查询,1:多:多,根据1查多再查多

1个代理人对应多个用户,用户对应多个订单

原生sql语句

1 $result = $users->query("select orderid,money,tid,username,game,type,ordertime 
2             from (
3                 (cmf_agent a inner join cmf_q_users b on a.id=b.agent_id)
4                 inner join cmf_orders c on c.userid=b.id
5             )where(
6                 agent_id = $agentid
7             )"
8         );

tp关联查询

 1 $count1 = $users
 2         ->field(‘orderid,money,tid,username,game,type,ordertime‘)
 3         ->join(‘cmf_agent on cmf_q_users.agent_id = cmf_agent.id‘)
 4         ->join(‘cmf_orders on cmf_orders.userid = cmf_q_users.id‘)
 5         ->where($where)
 6         ->count();
 7         // dump($count1);
 8 
 9         $page = $this->page($count1, 10);
10 
11         $result = $users
12         ->field(‘orderid,money,tid,username,game,type,ordertime‘)
13         ->join(‘cmf_agent on cmf_q_users.agent_id = cmf_agent.id‘)
14         ->join(‘cmf_orders on cmf_orders.userid = cmf_q_users.id‘)
15         ->where($where)
16         ->limit($page->firstRow . ‘,‘ . $page->listRows)
17         ->select();

 

3表查询,1:多:多,根据1查多再查多