ホームページ  >  記事  >  バックエンド開発  >  ThinkPHP クエリ ステートメントと関連クエリの使用法

ThinkPHP クエリ ステートメントと関連クエリの使用法

不言
不言オリジナル
2018-06-08 10:44:571432ブラウズ

この記事では、主に ThinkPHP のクエリ ステートメントと関連するクエリの使用方法、およびクエリ条件としての配列やクエリのオブジェクト メソッドなどのテクニックを含む一般的なクエリ メソッドを例の形式で紹介します。必要な方は参照してください。

この記事の例では、ThinkPHP クエリ ステートメントと関連クエリの使用法について説明します。皆さんの参考に共有してください。詳細は次のとおりです。

thinkphp フレームワーク ページでは、SQL クエリ ステートメントを直接記述して、データベース クエリの読み取りおよび書き込み操作を実装できます。これを説明する例を次に示します。

通常のクエリの文字列クエリ条件に加えて、配列およびオブジェクトのクエリ条件もよく使用され、これらは基本的なクエリに必要です。

1. クエリ条件として配列を使用します。

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

2. オブジェクト モードを使用してクエリを実行します。ここでは、stdClass 組み込みオブジェクトを例に挙げます。

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

1。文字列形式

$user=M('user');
$list=$user->where(&#39;id>5 and id<9&#39;)->select();
$list=$user->where($data)->select();

2。オブジェクト形式

$user=M(&#39;user&#39;);
$list=$user->where(array(&#39;username&#39;=>&#39;www.jb51.net&#39;))->select();
$list=$user->where($data)->select();

2 つのテーブル間の連想クエリ:

$user=M(&#39;user&#39;);
$a=new stdClass();
$a->username=&#39;www.jb51.net;
$list=$user->where($a)->select();

間隔クエリ

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

結合クエリ

$user=M(&#39;user&#39;);
$data[&#39;id&#39;]=array(array(&#39;gt&#39;,20),array(&#39;lt&#39;,23),&#39;and&#39;);
$list=$user->where($data)->select();

複合クエリ

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

3つのデータテーブルの関連クエリ

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;);
$data[&#39;password&#39;]=array(&#39;like&#39;,&#39;p%&#39;);
$data[&#39;_logic&#39;]=&#39;or&#39;;
$where[&#39;_complex&#39;]=$where;
$where[&#39;id&#39;]=array(&#39;lt&#39;,30);
$list=$user->where($data)->select();

データテーブルのクエリ条件

①以下は直接クエリです whereに条件を入れるので条件が書きやすくなります

$M_shopping = M(&#39;Shops&#39;);
$M_product = M(&#39;Product&#39;);
$M_proimg = M(&#39;Product_image&#39;);
$list_shops = $M_shopping->join(&#39;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&#39;)->fiel(&#39;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&#39;)->where("shops.user_cookie=&#39;".$_COOKIE[&#39;hr_think_userid&#39;]."&#39;")
->group(&#39;shops.id&#39;)->select();

② 上記以外に配列メソッドもあります

$m_test = M("Product");
$productmeaage = $m_test->where("p_id=&#39;$proid&#39;")->select();

上記これがこの記事の全内容です。学習が役立つことを願っています。その他の関連コンテンツについては、PHP 中国語 Web サイトに注目してください。

関連する推奨事項:

ThinkPHP の関連付けモデル

##ThinkPHP で一般的に使用されるクエリ言語


#

以上がThinkPHP クエリ ステートメントと関連クエリの使用法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。