ThinkPHP query statements and usage of related queries

不言
Release: 2023-03-30 17:08:02
Original
1381 people have browsed it

This article mainly introduces the usage of ThinkPHP query statements and related queries, and common query methods in the form of examples, including techniques such as arrays as query conditions and object methods for querying. Friends in need can refer to it

The examples in this article describe the usage of ThinkPHP query statements and related queries. Share it with everyone for your reference. The details are as follows:

In the thinkphp framework page, we can directly spell sql query statements to implement database query read and write operations. Here is an example to illustrate this.

In addition to string query conditions for ordinary queries, array and object query conditions are very commonly used, and these are necessary for basic queries.

1. Use arrays as query conditions

$User = M("User"); //实例化User对象 $condition['name'] = 'thinkphp'; // 把查询条件传入查询方法 $User->where($condition)->select();
Copy after login

2. Use object mode to query. You can use any object. Here we take the stdClass built-in object as an example.

$User = M("User"); // 实例化User对象 // 定义查询条件 $condition = new stdClass(); $condition->name = 'thinkphp'; // 查询name的值为thinkphp的记录 $User->where($condition)->select(); // 上面的查询条件等同于 where('name="thinkphp"') 使用对象方式查询和使用数组查询的效果是相同的,并且是可 带where条件的普通查询
Copy after login

1. String form

$user=M('user'); $list=$user->where('id>5 and id<9')->select(); $list=$user->where($data)->select();
Copy after login

2. Array format

$user=M('user'); $list=$user->where(array('username'=>'www.jb51.net'))->select(); $list=$user->where($data)->select();
Copy after login

3. Object format

$user=M('user'); $a=new stdClass(); $a->username='www.jb51.net; $list=$user->where($a)->select();
Copy after login

Associative query between two tables:

$M_shopping = M('Shops'); $M_product = M('Product'); $list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id') ->field('product.p_id,product.p_name,shops.product_amount,shops.product_id') ->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'") ->group('shops.id') ->select();
Copy after login

Interval query

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

Combined query

$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

Compound query

$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();
Copy after login

Associated query of three data tables

$M_shopping = M('Shops'); $M_product = M('Product'); $M_proimg = M('Product_image'); $list_shops = $M_shopping->join('as shops left join hr_product as product on shops.product_id = product.p_id left join hr_product_image as productimgon productimg.p_id = product.p_id')->fiel('productimg.pi_url,product.p_id,product.p_name,shops.product_amount,shops.product_id,product.am_id, product.p_procolor,product.p_price,product_amount*p_price as totalone')->where("shops.user_cookie='".$_COOKIE['hr_think_userid']."'") ->group('shops.id')->select();
Copy after login

Query conditions of the data table
① The following is a direct query The conditions are placed in where, which makes it easier to write the conditions

$m_test = M("Product"); $productmeaage = $m_test->where("p_id='$proid'")->select();
Copy after login

② In addition to the above method, there is also an array method

$M_product = M('Product'); $map['pid'] = $proid; $p_result = $M_product->where($map)->select();
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

ThinkPHP’s association model

##Query language commonly used in ThinkPHP

The above is the detailed content of ThinkPHP query statements and usage of related queries. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!