TP框架多表聯查

2022-05-18 06:30:42 字數 2593 閱讀 4080

join方法

import("@.org.page");

$form   =   m('gly');

$where='';

if ($_post['qcx']=='查詢')

}$count  = $form->join('think_role  on think_gly.rid = think_role.id')->where($where)->count('think_gly.id');    //計算總數

$page = new page($count, 10);

$list = $form->field('think_gly.*,think_role.name as rolename')->join('think_role on think_gly.rid=think_role.id' )->where($where)->limit($page->firstrow. ',' . $page->listrows)->order('think_role.id desc')->select();

// 模擬設定分頁額外傳入的引數

// 設定分頁顯示

$page->setconfig('header', '條資料');

$page->setconfig('first', '《上頁');

$page->setconfig('last', '下頁》');

$page = $page->show();

$this->assign("page", $page);

$this->assign("list", $list);

$this->display();

import("@.org.page");

$form   =   m('gly');

$where='';

if ($_post['qcx']=='查詢')

}$count  = $form->table('think_role a,think_gly b')->where('a.id=b.rid')->count('b.id');    //計算總數

$page = new page($count, 10);

$list = $form->field('b.*,a.name as rolename')->table('think_role a,think_gly b' )->where('a.id=b.rid')->limit($page->firstrow. ',' . $page->listrows)->order('b.id desc')->select();

// 模擬設定分頁額外傳入的引數

// 設定分頁顯示

$page->setconfig('header', '條資料');

$page->setconfig('first', '《上頁');

$page->setconfig('last', '下頁》');

$page = $page->show();

$this->assign("page", $page);

$this->assign("list", $list);

$this->display();

sql的執行效果如下:

select count(b.id) as tp_count from think_role a,think_gly b where a.id=b.rid limit 1;

select b.*,a.name as rolename from think_role a,think_gly b where a.id=b.rid order by b.id desc limit 0,10

參考的資料如下:

thinkphp 中關聯查詢(多表查詢)可以使用 table() 方法或和join方法,請看示例:

聯合查詢

1、原生查詢

$sql = 'select f.*,s.sort_name from think_form as f, think_sort as s where f.sort_id=s.sort_id  order by f.id desc limit 3';

$list = $form->query($sql);

2、join() 兩表查詢

$list = $form->join('think_sort on think_form.sort_id = think_sort.sort_id' )->select();

3、join() 多表查詢

$list = $form->join('think_sort on think_form.sort_id = think_sort.sort_id' )->join('think_brand on think_form.brand_id = think_brand.brand_id' )->select();

4、table()

$list = $user->table('user_status stats, user_profile profile')->where('stats.id = profile.typeid')->field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')->order('stats.id desc' )->select();

mysql 多表聯查 MySQL的多表聯查

今天是周二,我們一起來研究下mysql的多表聯查啊。或許你也知道,表之間的關係有 1對1 1對多 多對多。然後.1.巢狀查詢 乙個查詢的結果是另外sql查詢的條件 如 查詢stu表中年齡最大的是誰?mysql select from stu where age select max age from...

mysql多表聯查 mysql 多表聯查 例項

多表查詢 笛卡爾積查詢 笛卡爾積查詢 就是兩張表相乘,若左邊表有m條資訊,右邊表有n條資訊,那麼查詢顯示的資訊總共為m n條,這其中往往包含大量錯誤資料,需要用where 條件來過濾無用資訊 笛卡爾積查詢語句 select from dept,emp id name id name dept id ...

mysql 多表聯查

1.多表連線型別 2.1.笛卡爾積 交叉連線 在 mysql 中可以為 cross join 或者省略 cross 即 join,或 者使用 如 1.select from table1 cross join table2 2.select from table1 join table2 3.sel...