Table of Contents
ThinkPHP中的常用查询语言汇总,thinkphp语言汇总
thinkphp查询所有的信息,整个表都查,取出某个字段
thinkphp 中的查询语言 疑问!
Home php教程 php手册 ThinkPHP中的常用查询语言汇总,thinkphp语言汇总

ThinkPHP中的常用查询语言汇总,thinkphp语言汇总

Jun 13, 2016 am 09:26 AM
thinkphp Inquire language

ThinkPHP中的常用查询语言汇总,thinkphp语言汇总

本文实例汇总了ThinkPHP中的常用查询语言,供大家参考之用。相信能给大家ThinkPHP开发带来一定的帮助。具体如下:

一、普通查询:

在查询带入where条件等,最少有三种形式

1、字符串形式:

'id>5 and id<9'

Copy after login

2、数组形式:

示例代码如下:

$user=M('user');
$data['username']='liwenkai';
$list=$user->where(array('username'=>'liwenkai'))->select();
$list=$user->where($data)->select();

Copy after login

3、对象形式:

示例代码如下:

$user=M('user');
$a=new stdClass();
$a->username='liwenkai';
$list=$user->where($a)->select();  

Copy after login

4、查询表达式:

EQ 等于
NEQ 不等于
GT 大于
EGT 大于等于
LT 小于
ELT 小于等于
LIKE 等价与sql中的like
[NOT] BETWEEN 查询区间
[NOT] IN 查询集合
EXP 指使用标准SQL语句,实现更加复杂的情况

常用形式:

$data['字段名']=array('是表达式','查询条件');
Copy after login

此外

$data['liwenkai']='liwenkai';
Copy after login

实际上等价于

$data['liwenkai']=array('eq','liwenkai');
Copy after login

示例如下:

$data['username']=array('like','peng%');
$list=$user->where($data)->select();

Copy after login

二、区间查询:

示例如下:

$user=M('user');
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();
dump($list);
Copy after login

$data['username']=array(array('like','p%'),array('like','h%'),'or');

Copy after login

三、组合查询:

示例如下:

$user=M('user');
$data['username']='pengyanjie';
$data['password']=array('eq','pengyanjie');
$data['id']=array('lt',30);
$data['_logic']='or';
$list=$user->where($data)->select();
dump($list);

Copy after login

四、复合查询:

示例如下:

$user=M('user');
$data['username']=array('eq','pengyanjie');
$data['password']=array('like','p%');
$data['_logic']='or';
$where['_complex']=$where;
$where['id']=array('lt',30);
$list=$user->where($data)->select();
dump($list);
Copy after login

相当于

(id<30)and ( (username=pengyanjie) or (password like p%) )
Copy after login

五、统计查询:

示例如下:

echo $user->count();
echo '<br>';
echo $user->max('id');
echo '<br>';
echo $user->where('id<30')->min('id');
echo '<br>';
echo $user->avg('id');
echo '<br>';
echo $user->sum('id');

Copy after login

六、定位查询:

示例如下:

$user=new AdvModel('user');//实例化高级模型AdvModel
//$user=M('user','CommonModel');//或者将AdvModel用CommonModel来继承
$list=$user->order('id desc')->getN(2);//返回结果中的第三条
dump($list);

$list=$user->order('id desc')->last();//返回最后一条
$list=$user->order('id desc')->first();//返回第一条

Copy after login

七、SQL查询:

1.excute()主要用于更新和写入:

$Model = new Model() // 实例化一个 model 对象  没有对应任何数据表
$Model->execute( "update think_user set name='thinkPHP' where status=1" );

Copy after login

2.query()主要用于查询:

$user=M();
$list=$user->query('select * from aoli_user order by id desc');
dump($list);         
Copy after login

八、动态查询

示例如下:

$user=M('user');
$list=$user->getByusername('pengyanjie');
$list=$user->getByusername('pengyanjie');
dump($list);

Copy after login

$user=new AdvModel('user');
$list=$user->top5();//前5条
dump($list);

Copy after login

感兴趣的朋友可以在ThinkPHP项目中调试运行本文示例,相信会有新的收获。

thinkphp查询所有的信息,整个表都查,取出某个字段

$res=$student->field('id,name')->select();/*你要查询出所有的id和name 那么条件就不写了,否则不能查询出所有的最好在后面加个limit限制下数据量,万一是大型网站,海量数据,你的查询工具就卡了,浏览器刷屏了!*/

 

thinkphp 中的查询语言 疑问!

随着使用的深入,之前很多疑问都会不断的解开。
初学者不要抱着批判的眼光,每个功能都有其实际的使用意义,初期选用自己觉得喜欢的方法就行,等到了高手的层次再来挑刺。
 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages ​​respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same

See all articles