thinkphp5.1查询表达式
web ThinkPHP5.1 2019-01-08 2661 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6443362 2613 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
查询表达式
查询表达式支持大部分的SQL查询语法,也是ThinkPHP查询语言的精髓,查询表达式的使用格式:
where('字段名','表达式','查询条件');
whereOr('字段名','表达式','查询条件');
5.1还支持新的查询方法
whereField('表达式','查询条件');
whereOrField('表达式','查询条件');
Field使用字段的驼峰命名方式。

whereField的意思就是where+字段名,

比如字段名是name,那么就是whereName('=','张三')

比如字段名是articleId,那么就是whereArticleid('=',100) 注意不能写成whereArticleId,否则字段名会被解析成article_id


表达式不分大小写,支持的查询表达式有下面几种:

表达式 含义 快捷查询方法
= 等于
<> 不等于
> 大于
>= 大于等于
< 小于
<= 小于等于
[NOT] LIKE 模糊查询 whereLike/whereNotLike
[NOT] BETWEEN (不在)区间查询 whereBetween/whereNotBetween
[NOT] IN (不在)IN 查询 whereIn/whereNotIn
[NOT] NULL 查询字段是否(不)是NULL whereNull/whereNotNull
[NOT] EXISTS EXISTS查询 whereExists/whereNotExists
[NOT] REGEXP 正则(不)匹配查询(仅支持Mysql)
[NOT] BETWEEM TIME 时间区间比较 whereBetweenTime
> TIME 大于某个时间 whereTime
< TIME 小于某个时间 whereTime
>= TIME 大于等于某个时间 whereTime
<= TIME 小于等于某个时间 whereTime
EXP 表达式查询,支持SQL语法 whereExp


<?php
namespace app\index\model;

use think\Db;
class Articles
{
	/**
	 * 查询表达式
	 * 等于(=)
	 */
	public function where(){
		// 等于(=)
		$rs = Db::name('articles')->where('articleId','=',10)->select();
		// 和下面的查询等效
		$rs = Db::name('articles')->where('articleId',10)->select();
		// 不等于(<>)
		$rs = Db::name('articles')->where('articleId','<>',10)->select();
		// 大于(>)
		$rs = Db::name('articles')->where('articleId','>',10)->select();
		dump($rs);
		exit;
		return $rs;
	}	
	/**
	 * 查询表达式
	 * [NOT] LIKE: 同sql的LIKE
	 */
	public function where2(){
		// like
		$rs = Db::name('articles')->where('articleTitle','like','新闻%')->select();
		// not like
		$rs = Db::name('articles')->where('articleTitle','not like','新闻%')->select();
		// like查询支持使用数组-OR
		$rs = Db::name('articles')->where('articleTitle','like',['新闻%','货到%'],'OR')->select();
		// like查询支持使用数组-AND
		$rs = Db::name('articles')->where('articleTitle','like',['%付款','货到%'],'AND')->select();
		// 为了更加方便,应该直接使用whereLike方法
		$rs = Db::name('articles')->whereLike('articleTitle','新闻%')->select();
		// 为了更加方便,应该直接使用whereLike方法-whereNotLike
		$rs = Db::name('articles')->whereNotLike('articleTitle','新闻%')->select();
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 查询表达式
	 * [NOT] BETWEEN :同sql的[not] between
	 */
	public function where3(){
		// 查询条件支持字符串或者数组-字符串
		$rs = Db::name('articles')->where('articleId','between','5,10')->select();
		// 查询条件支持字符串或者数组-数组
		$rs = Db::name('articles')->where('articleId','between',[5,10])->select();
		// 查询条件支持字符串或者数组-not between
		$rs = Db::name('articles')->where('articleId','not between','5,10')->select();
		// 最快捷的查询方法-whereBetween
		$rs = Db::name('articles')->whereBetween('articleId','5,7')->select();
		// 最快捷的查询方法-whereNotBetween
		$rs = Db::name('articles')->whereNotBetween('articleId','5,7')->select();
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 查询表达式
	 * [NOT] IN: 同sql的[not] in
	 */
	public function where4(){
		// 查询条件支持字符串或者数组-字符串
		$rs = Db::name('articles')->where('articleId','in','5,10')->select();
		// 查询条件支持字符串或者数组-数组
		$rs = Db::name('articles')->where('articleId','in',[5,10])->select();
		// 查询条件支持字符串或者数组-not in
		$rs = Db::name('articles')->where('articleId','not in','5,10')->select();
		// 最快捷的查询方法-whereIn
		$rs = Db::name('articles')->whereIn('articleId','5,7')->select();
		// 最快捷的查询方法-whereNotIn
		$rs = Db::name('articles')->whereNotIn('articleId','5,7')->select();
		// [NOT] IN查询支持使用闭包方式-试着写一个,可以运行,不知道是不是正确使用方式
		$sNum = 5;
		$bNum = 7;
		$rs = Db::name('articles')->where(function ($query) use($sNum,$bNum) {
			$query->whereIn('articleId', [$sNum,$bNum]);
		})->select();
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
	/**
	 * 查询表达式
	 * [NOT] NULL-查询字段是否(不)是Null
	 */
	public function where5(){
		// 查询字段是否(不)是Null
		$rs = Db::name('articles')
			->where('articleTitle',null)
			->where('articleContent','null')
			->where('delete_time','not null')
			->select();
		// 如果你需要查询一个字段的值为字符串null或者not null,应该使用
		$rs = Db::name('articles')
			->where('articleTitle','=','null')
			->where('articleContent','=','not null')
			->select();
		// 推荐的方式是使用whereNull和whereNotNull方法查询。
		$rs = Db::name('articles')
			->whereNull('articleTitle')
			->whereNotNull('delete_time')
			->select();
		// EXP:表达式-exp查询的条件不会被当成字符串,所以后面的查询条件可以使用任何SQL支持的语法,包括使用函数和字段名称。
		$rs = Db::name('articles')
			->where('articleId','exp','in(5,6,7)')
			->select();
		// 动态查询-不能使用whereArticleId,会把字段名称解析为article_id导致找不到字段
		$rs = Db::name('articles')
			->whereArticleid('>=',10)
			->select();
		echo Db::getlastsql();
		dump($rs);
		exit;
		return $rs;
	}
} 

版权声明:本篇文章来源于网络。 来源链接

相关推荐
Non-static method think\Config::get() should not be called statically
ThinkPHP5.1 | 2018-12-29 10415
原来是这样use think\Config; 改成这样use think\facade\Config; 下面是官方手册的解释 配置获取 要使用Config类,首先需要在你的类文件中引入 use think\facade\Config; 或者(因为系统做了类库别名,其实就是调用think\facade\Config) u...
thinkPHP5 order多条件排序
ThinkPHP5.1 | 2017-10-24 10284
总结如下: //单一条件排序 $user = $this->where(['parentId'=>0)->field('userId,userName,userSort,isShow')->order('userSort', 'asc')->select(); //多个条件排序,可以多加一个order...
评论:0条
评论加载中...
发表评论