首页 > php框架 > ThinkPHP > 正文

thinkphp怎么查询多个数据

藏色散人
发布: 2022-12-05 09:26:45
原创
2351人浏览过
thinkphp查询多个数据的方法:1、使用Table方法进行多表查询,语法如“$Model->table('think_blog blog,think_type type')”;2、使用Join方法进行查询,代码如“$Model->join('work ON artist.id = work.artist_id')”。

thinkphp怎么查询多个数据

本教程操作环境:Windows7系统、ThinkPHP5版、Dell G3电脑。

thinkphp怎么查询多个数据?

THINKPHP 中关联查询(多表查询)

THINKPHP 中关联查询(多表查询)可以使用 table() 方法或和join方法,请看示例:

立即学习PHP免费学习笔记(深入)”;

1、Table方法:定义要操作的数据表名称,可以动态改变当前操作的数据表名称,需要写数据表的全名,包含前缀,可以使用别名,例如:

$Model->Table('think_user user')
->where('status>1')
->select();
$Model->table('think_blog blog,think_type type')
->where('blog.typeid=type.id')
->field('blog.id as id,blog.title,blog.content,type.typename as type')
->order('blog.id desc' )
->limit(5)
->select();
登录后复制

Table方法的参数支持字符串和数组,数组方式的用法:

$Model->Table(array('think_user'=>'user','think_group'=>'group'))
->where('status>1')
->select();
登录后复制

使用数组方式定义的优势是可以避免因为表名和关键字冲突而出错的情况。

 

蓝心千询
蓝心千询

蓝心千询是vivo推出的一个多功能AI智能助手

蓝心千询 34
查看详情 蓝心千询

注:如果不定义table方法,默认会自动获取当前模型对应或者定义的数据表。

 

2、Join方法:查询Join支持,Join方法的参数支持字符串和数组,并且join方法是连贯操作中唯一可以多次调用的方法。例如:

$Model->join('work ON artist.id = work.artist_id')
->join('card ON artist.card_id = card.id')
->select();
//Left Join
$Model->table('user U')
->join('news N on U.id=N.cid')
->field('U.*,N.*')
->order('id desc')
->limit('8')
->findall();
登录后复制

 

默认采用LEFT JOIN 方式,如果需要用其他的JOIN方式,可以改成

$Model->join('RIGHT JOIN work ON artist.id = work.artist_id')
->select();
//Right Join
$Model->table('user U')
->join(array('right','news N on U.id=N.cid'))
->field('U.*,N.*')
->order('id desc')
->limit('8')
->findall();
登录后复制

如果join方法的参数用数组的话,只能使用一次join方法,并且不能和字符串方式混合使用。

$Model->join(array(' work ON artist.id = work.artist_id', 'card ON artist.card_id = card.id'))
->select()
登录后复制

 

运用这种连贯操作方法,可以有效的提高数据查询的代码清晰度和开发效率。

 

查看连贯操作的SQL语句的方法:

echo $Model->getLastSql(); //打印一下SQL语句,查看一下
登录后复制

例2:

1、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();
登录后复制

2.1、join()2表查询

$user = new Model('user');
$list = $user->join('RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' );
登录后复制

2.2、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();
登录后复制

3、原生查询

$Model = new Model();
$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p->firstRow.','.$p->listRows;
$voList = $Model->query($sql);
登录后复制

推荐学习:《thinkPHP视频教程

以上就是thinkphp怎么查询多个数据的详细内容,更多请关注php中文网其它相关文章!

相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号