博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThinkPhp数据库查询
阅读量:4199 次
发布时间:2019-05-26

本文共 2687 字,大约阅读时间需要 8 分钟。

方法一:常规方法(适应于单表查询)

数据库
$Form = M("Form"); 
// 按照id排序显示前6条记录 
$list = $Form->order('id desc')->limit(6)->select(); 
$this->assign('list', $list); 
$this->display();
表单操作
$Form = D("Form");
if ($Form->create()) {
    if (false !== $Form->add()) {
        $this->success('数据添加成功!');
    } else {
        $this->error('数据写入错误');
    }
} else {
    header("Content-Type:text/html; charset=utf-8");
    exit($Form->getError() . ' [ <A HREF="javascript:history.back()">返 回</A> ]');
}

方法二:视图方法(适用于多表查询)

先在Model创建视图模型类BlogViewModel.class.php文件的部分代码:

class BlogViewModel extends ViewModel {
    public $viewFields = array(
        'Blog'=>array('id','name','title','keywords','description','cTime','categoryId','content','readCount','tags','commentCount','status'),
        'Category' => array('title'=>'category', '_on'=>'Blog.categoryId=Category.id')
    );
}
代码说明:
$viewFields:就是视图表的关联二维数组,每个数组说明结合的一个表;
'Category'  =>  array('title'=>'category', '_on'=>'Blog.categoryId=Category.id'),
这段代码中的'_on'就是表连接查询类似where条件,'title'=>'category'意思是在查询的结果以category访问对应的文章类别
title就是Category表要显示的字段,category是title字段显示的名称。等同于 title as category
在Action里面使用
$Blog = D("BlogView");     //调用视图模型类
$result = $Blog->where('Blog.id='.$id)->find();

方法三:表格方法

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(); 
使用数组方式定义的优势是可以避免因为表名和关键字冲突而出错的情况。。
注:如果不定义table方法,默认会自动获取当前模型对应或者定义的数据表。

方法四: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()    方法五:原生查询
$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); 转载自:http://ht19820316.blog.163.com/blog/static/3395523320124103330707/
你可能感兴趣的文章
WIN7下开启无线网卡软AP
查看>>
Unofficial Windows Binaries for Python Extension Packages
查看>>
CShell 简单语法
查看>>
Linux(CentOS)下把python脚本转化成可执行程序
查看>>
【Unity3D游戏开发】性能优化之Texture图片空间和内存占用分析(三七)
查看>>
【Unity3D游戏开发】material与sharedMaterial的区别 (三八)
查看>>
【Unity2D游戏实战 之 2D滚屏射击】1.背景滚动 (一)
查看>>
【Git+Source Tree使用教程之一】commit & push
查看>>
C#和.NET框架和术语
查看>>
【React Native】把现代web科技带给移动开发者(一)
查看>>
【GoLang】Web工作方式
查看>>
Launch Sublime Text 3 from the command line
查看>>
【数据库之mysql】mysql的安装(一)
查看>>
【数据库之mysql】 mysql 入门教程(二)
查看>>
【HTML5/CSS/JS】A list of Font Awesome icons and their CSS content values(一)
查看>>
【HTML5/CSS/JS】<br>与<p>标签区别(二)
查看>>
【HTML5/CSS/JS】开发跨平台应用工具的选择(三)
查看>>
【心灵鸡汤】Give it five minutes不要让一个好主意随风而去
查看>>
【React Native】Invariant Violation: Application AwesomeProject has not been registered
查看>>
【ReactNative】真机上无法调试 could not connect to development server
查看>>