SQL multi-table query data merging and paging details multiple tables with different structures

Source: Internet
Author: User
If the structures of multiple tables are different, it seems impossible to query the results of multiple tables together! For example, Table

If the structures of multiple tables are different, it seems impossible to query the results of multiple tables together!

For example, table:

A_id Title Is_open Last_update
2 Test 1 1 130225252

Table B:

B _id Name Is_check Last_update
2 B Test 2 0 156523322

In this way, two tables with different structures can be displayed in a list.


First, we create a variable: $ moretable


$ Moretable = array ('a table' => array ('where' => 'is _ open', 'zone '=> 'Title as name, a_id as checkid, last_update ', 'URL' => 'a1. php', 'keyword' => 'title'), 'B table' => array ('where' => 'is _ check ', 'zone '=> 'name as name, B _id as checkid, last_update', 'URL' => 'b1. php', 'keyword' => 'name '))

Then SQL. obtain the total number of data:

Create a function in php first


function get_alltable_count($table=array(),$type=0,$user_id,$keywords=''){$info=0;if (is_array($table)){foreach ($table as $key=>$val){$sql ="SELECT COUNT(*) FROM ".$GLOBALS['sdk']->table($key)." WHERE $val[where] = '$type' ";            if (!empty($keywords))            {            $sql.="AND $val[keyword] LIKE '%" . mysql_like_quote($keywords) . "%' ";            }$sql .="AND user_id = '$user_id' ";$info += $GLOBALS['db']->getOne($sql);}return $info;}else {return $info;}}

Then, obtain the total number of data:

$record_count = get_alltable_count($GLOBALS['moretable'],0,$user_id,$filter['keyword']);

Then, obtain the data:


function get_alltable_infos($table=array(),$type=0,$user_id,$num = 10, $start = 0, $keywords=''){$arr=array();if (is_array($table)){foreach ($table as $key=>$val){$sql = "SELECT $val[zone] FROM ".$GLOBALS['sdk']->table($key)." WHERE $val[where] = '$type' "; if (!empty($keywords))            {            $sql.="AND $val[keyword] LIKE '%" . mysql_like_quote($keywords) . "%' ";            }$sql.="AND user_id = '$user_id' ORDER BY last_update DESC";$res=$GLOBALS['db']->query($sql);while ($row = $GLOBALS['db']->fetchRow($res)) {$row['url'] = $val['url']."?act=edit&id=".$row['checkid'];$row['last_update'] = local_date($GLOBALS['_CFG']['time_format'],$row['last_update']);;$arr[]=$row;}}$arr=array_slice($arr,$start,$num);}return $arr;}

Paging functions can be written by yourself. with the total number of data pages, it is easy to write.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.