Record a use of sql union syntax

In the old project, the contents in the two completely independent tables need to be extracted and displayed under the same category. And it needs to not affect the use of existing functions of the old project and to quickly modify and go online .
The first thing that comes to my mind is: try to avoid drastically modifying the old project code (fragile products).
At this time, the most easy-to-understand modification plan popped up in my mind: query the content that needs to be reflected in the two tables respectively, and put the data Back to the foreground, the foreground performs data rendering by reading the data of the two interfaces.
However,
this seemingly very simple process is not the best solution if you think about it. For example:
1. The front-end needs to request two different interfaces to get the data to be combined, and the fields of the two tables are not uniform
. 2. When the front-end renders the data for paging, the calculation of the total number and the number of pages is not very good. (The paging function realized by the plug-in used in the front-end)
3. The front-end responsible for the old project has left. As a back-end program, I have to be familiar with the plug-in before modifying the page. The risk of error increases.
Therefore, I think of another solution:
if There can be a way to query the data of two different tables at the same time to merge and return, so that you only need to modify the interface file and a small amount of front-end code.
After checking, there really is such a method. The union syntax of sql, I turned over the manual of tp, and understood the usage of union. The example is very simple.

UNION操作用于合并两个或多个 SELECT 语句的结果集
Db::field('name')
      ->table('think_user_0')
      ->union('SELECT name FROM think_user_1')
      ->union('SELECT name FROM think_user_2')
      ->select();

It's very simple, but because I have never used this grammar before, I still need to learn more and continue to study the basic usage of union:
1. The premise of using union, the column corresponding to the SELECT statement in UNION must have similar data Type
such as: two tables table1 (field aid aname atel) and table2 (field bid bname btel), I want to query the id and name values ​​of the two tables, then the field type of aid and bid is the same, and the data type of ananme and bname is the same. use.
2. The number of fields in the two tables to be queried by union must be the same, and the order of the fields must be one-to-one, such as:

Db::field('aid,aname')
      ->table('table1')
      ->union('SELECT bid,bname FROM table2')
      ->select();

3 Sorting, conditions, and paging queries are still written according to what they should be written before, such as:

Db::field('aid,aname')
      ->table('table1')
      ->union('SELECT bid,bname FROM table2 WHERE bsex="1"')//条件
      ->order('bcreatetime desc')//排序
      ->page($p,$listpage)//分页,根据自己的分页来
      ->select();

After understanding the basic usage, for the situation of my project, these are enough, and start to modify.
Although the two table fields of the old project do not completely correspond, most of the field types are still corresponding. I only need to query the corresponding fields and return them one by one, which is convenient for the front-end rendering data and separate the fields of the two tables. They all have the same alias, such as aid as id, bid as id. The comprehensive data returned in this way can be directly rendered with id. It is convenient for the front end to paging, and the interface can directly process the paging data and return it to the front end. Query the data quantity of the two tables respectively, and then do the sum to calculate the number of pages and quantity.

public function index(){
    
    
    $p=input('p',1,'intval');//页面
     $listpage=input('listpage',10,'intval');//分页大小
     $dlist=Db::field('id,dname name,dimages image,dcreatetime pcreatetime,1 type,dcontent content')//换成自己查询的字段即可
         ->table('cen_deal')
         ->union('SELECT id,pname name,pimages image,pcreatetime,2 type,pcontent content FROM cen_collection WHERE cjlist="2"')
	  ->page($p,$listpage)
	  ->order('pcreatetime desc')
         ->select();
	$count=Db::name('ddeal')->order('dcreatetime desc')->count();
	$count2=Db::name('ccollection')->order('pcreatetime desc')->where('cjlist','2')->count();
	$countzh=$count+$count2;
	$totalPage=ceil($countzh/$listpage);
      if($dlist){
    
    
          return json(['data'=>$dlist,'code'=>1,'message'=>'success','totalPage'=>$totalPage,'count'=>$countzh,'p'=>$p]);
      }else{
    
    
          return json(['data'=>'无数据','code'=>2,'message'=>'fail']);
      }
}

In this way, the front end only needs to modify the rendered field (modified to the alias after as), and it can change from displaying the content of one table to displaying the content of two unrelated tables.

Guess you like

Origin blog.csdn.net/qq_36129701/article/details/90768667