TP6关于union查询请查看官方文档:https://www.kancloud.cn/manual/thinkphp6_0/1037550/
这里以个人实现方法,比如有表1=table1,表2=table2,表3=table3,且都有相同字段编号=number,名称=name,手机=mobile,然后我们查关键词为“哈喽吧”,实现代码如下:
$sql=" where 1=1 "; $map=[]; $key=$_GET['key']; //key=哈喽吧 if($key!=''){ $sql.="and (name like '%{$key}%')"; $map[]=['name','like',"%{$key}%"]; } //统计总数 $total = Db::query("select sum(a.b) as num from ( select count(1) as b from table1 {$sql} UNION ALL select count(1) as b from table2 {$sql} UNION ALL select count(1) as b from table3 {$sql} ) as a")[0]['num']; //分页输出记录集 $list=Db::table(' table1') ->field('number,name,mobile') ->unionAll('SELECT number,name,mobile FROM table2'.$sql) ->unionAll('SELECT number,name,mobile FROM table3'.$sql) ->where($map) ->page(1,10) ->select();
上一篇:TP6提示错误: Call to undefined function think\response\finfo_open()
下一篇:TP6常用模板变量和语句输出
讨论数量:0