目錄
1 語法
3.1 对比
3.2 and
3.3 or
3.4 not
3.5 between和not between
3.6 in和not in
3.7 like
3.8 exists
首頁 後端開發 php教程 Yii2查詢where條件組裝詳解

Yii2查詢where條件組裝詳解

Mar 27, 2018 pm 01:41 PM
where yii2

<p>熟悉Yii2的查詢條件後,用Active Record查詢資料非常方便。 以下我們介紹where()方法當中,條件的拼裝方式。 </p> <h2 id="語法">1 語法</h2> <p style="white-space:pre-wrap;">Yii2用<code class="hljs bash" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-built_in">where</span>()</code>方法(當然還有其他方法)來實作條件篩選,語法:##<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>public $this where ( $condition, $params = [] )</pre><div class="contentsignin">登入後複製</div></div> </p><p style="white-space:pre-wrap;"><code class="hljs bash" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">$params<span class="hljs-variable" style="color:rgb(223,80,0);"></span>為可選參數,指定要綁定查詢的值。 </code></p><p style="white-space:pre-wrap;"><code class="hljs bash" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">$condition<span class="hljs-variable" style="color:rgb(223,80,0);"></span>為必選參數,</code><code class="hljs bash" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">$condition<span class="hljs-variable" style="color:rgb(223,80,0);"></span>可以是字串(如</code><code class="hljs bash" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">'id=1'<span class="hljs-string" style="color:rgb(223,80,0);"></span>)或陣列。 </code></p><p style="white-space:pre-wrap;"><code class="hljs bash" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">$condition<span class="hljs-variable" style="color:rgb(223,80,0);"></span>當陣列時,有兩種格式:</code></p><ul style="margin-top:0px;list-style-position:inside;" class=" list-paddingleft-2"><li>雜湊格式:<p>[' column1' => value1, 'column2' => value2, ...]<code class="hljs json" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;white-space:pre-wrap;"></code></p></li><li>#運算子格式:<p>[operator, operand1, operand2, ...]<code class="hljs json" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;white-space:pre-wrap;"></code></p></li></ul>2 哈希格式<h2 style="font-family:'Microsoft YaHei UI Light', SimHei, SimSun, sans-serif;line-height:1.1;color:rgb(66,139,209);"></h2>通常,哈希格式的查詢條件產生這樣的SQL語句:<p style="white-space:pre-wrap;"> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>column1=value1 AND column2=value2 AND ...</pre><div class="contentsignin">登入後複製</div></div></p>如果某個值是數組,就會產生<p style="white-space:pre-wrap;">IN<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">語句。 </code></p>如果某個值為<p style="white-space:pre-wrap;"><code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">null<span class="hljs-literal" style="color:rgb(0,134,179);"></span>,會用</code>IS<code class="hljs cpp" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">NULL<span class="hljs-literal" style="color:rgb(0,134,179);"></span>來產生語句。 </code></p>範例:<p style="white-space:pre-wrap;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;type&#39; => 1, &#39;status&#39; => 2] // 生成:(type = 1) AND (status = 2)[&#39;id&#39; => [1, 2, 3], &#39;status&#39; => 2] // 生成:(id IN (1, 2, 3)) AND (status = 2)[&#39;status&#39; => null] // 生成:status IS NULL</pre><div class="contentsignin">登入後複製</div></div></p>3 運算子格式<h2 style="font-family:'Microsoft YaHei UI Light', SimHei, SimSun, sans-serif;line-height:1.1;color:rgb(66,139,209);"></h2>在運算子格式,Yii會根據指定的運算子產生SQL語句。 <p style="white-space:pre-wrap;"><p style="white-space:pre-wrap;">運算子有:<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">和</span></code>、<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">或</span></code>、<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">不是</span></code>、<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">#之間</code>、<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">不</span> 之間</code>、<code class="hljs bash" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">中</span></code>、<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">不</span><span class="hljs-keyword" style="color:rgb(167,29,93);"></span></code>、<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">像</code>、## ####或### 喜歡<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">#、<span class="hljs-keyword" style="color:rgb(167,29,93);"></span>不</code> 喜歡<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">#、<span class="hljs-keyword" style="color:rgb(167,29,93);"></span>或</code><code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">不<span class="hljs-keyword" style="color:rgb(167,29,93);"># 喜歡</span>#、<span class="hljs-keyword" style="color:rgb(167,29,93);"></span>存在</code>##、<code class="hljs perl" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">不</span></code>存在<code class="hljs perl" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"></code>、<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">></code>、<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><</code>、<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">=</code>、<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">>=</code>、<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><=</code>、<code class="hljs diff" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-addition" style="color:rgb(85,165,50);background-color:rgb(234,255,234);">!=</span></code>等。</p><h3 id="对比">3.1 对比</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;>&#39;, &#39;id&#39;, 1] // 生成:id > 1[&#39;<&#39;, &#39;id&#39;, 100] // 生成:id < 100[&#39;=&#39;, &#39;id&#39;, 10] // 生成:id = 10[&#39;>=&#39;, &#39;id&#39;, 1] // 生成:id >= 1[&#39;<=&#39;, &#39;id&#39;, 100] // 生成:id != 10</pre><div class="contentsignin">登入後複製</div></div><p style="white-space:pre-wrap;">具体生成的SQL语句,运算符<code class="hljs objectivec" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">id</span></code>会自动加上反斜杠引号<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">`</code>,运算数会自动转义。</p><h3 id="and">3.2 and</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;and&#39;, &#39;id&#39; => 1, &#39;id&#39; => 2] // 生成:id=1 AND id=2[&#39;and&#39;, &#39;id=1&#39;, &#39;id=2&#39;] // 生成:id=1 AND id=2[&#39;and&#39;, &#39;type=1&#39;, [&#39;or&#39;, &#39;id=1&#39;, &#39;id=2&#39;]] // 生成:type=1 AND (id=1 OR id=2)</pre><div class="contentsignin">登入後複製</div></div><p style="white-space:pre-wrap;">在第2条和第3条语句中,列名称和搜索值未用键值关系指定,所以生成的SQL不会添加引号,也不会转义。</p><h3 id="or">3.3 or</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;or&#39;, [&#39;type&#39; => [7, 8, 9]], [&#39;id&#39; => [1, 2, 3]]] // 生成:(type IN (7, 8, 9) OR (id IN (1, 2, 3)))</pre><div class="contentsignin">登入後複製</div></div><h3 id="not">3.4 not</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;not&#39;, [&#39;attribute&#39; => null]] // 生成:NOT (attribute IS NULL)</pre><div class="contentsignin">登入後複製</div></div><h3 id="between和not-between">3.5 between和not between</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;between&#39;, &#39;id&#39;, 1, 10] // 生成:id BETWEEN 1 AND 10[&#39;not between&#39;, &#39;id&#39;, 1, 10] // 生成:id NOT BETWEEN 1 AND 10</pre><div class="contentsignin">登入後複製</div></div><p style="white-space:pre-wrap;">运算符后面的运算数1为数据表<span style="font-weight:bolder;">列名称</span>,运算数2和运算数3分别为列值范围的<span style="font-weight:bolder;">最小值</span>和<span style="font-weight:bolder;">最大值</span>。</p><h3 id="in和not-in">3.6 in和not in</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;in&#39;, &#39;id&#39;, [1, 2, 3]] // 生成:id IN (1, 2, 3)[&#39;not in&#39;, &#39;id&#39;, [1, 2, 3]] // 生成:id NOT IN (1, 2, 3)</pre><div class="contentsignin">登入後複製</div></div><p style="white-space:pre-wrap;">运算符后面的<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">运算数1</code>为列名称或DB表达式,<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">运算数2</code>为数组,指定列值所在的范围。</p><p style="white-space:pre-wrap;">这个方法会为值添加引号,并正确转义。</p><p style="white-space:pre-wrap;">要生成混合<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">IN</code>条件,列名和列值都设置为数组,并且用列名为列值指定下标:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;in&#39;, [&#39;id&#39;, &#39;name&#39;], [[&#39;id&#39; => 1, &#39;name&#39; => &#39;foo&#39;], [&#39;id&#39; => 2, &#39;name&#39; => &#39;bar&#39;]]] // 生成:(`id`, `name`) IN ((1, &#39;foo&#39;), (2, &#39;bar&#39;))</pre><div class="contentsignin">登入後複製</div></div><p style="white-space:pre-wrap;">另外,还可以用子查询作为<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">IN</code>条件的值,如下:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;in&#39;, &#39;user_id&#39;, (new Query())->select(&#39;id&#39;)->from(&#39;users&#39;)->where([&#39;active&#39; => 1])]</pre><div class="contentsignin">登入後複製</div></div><h3 id="like">3.7 like</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;like&#39;, &#39;name&#39;, &#39;tester&#39;] // 生成:name LIKE &#39;%tester%&#39;[&#39;like&#39;, &#39;name&#39;, [&#39;test&#39;, &#39;sample&#39;]] // 生成:name LIKE &#39;%test%&#39; AND name LIKE &#39;%sample%&#39;[&#39;like&#39;, &#39;name&#39;, &#39;%tester&#39;, false] // 生成:name LIKE &#39;%tester&#39; // 这是自定义查询方式,要传入值为false的运算数3,并且自行添加%</pre><div class="contentsignin">登入後複製</div></div><p style="white-space:pre-wrap;"><span style="color:rgb(119,119,119);font-family:'courier new';"><span style="background-color:rgb(238,238,238);">运算数</span></span>后面的运算数1为列名称或DB表达式,运算数2为字符串或数组,指定列值查询条件。</p><p style="white-space:pre-wrap;">这个方法会为值添加引号,并正确转义。</p><p style="white-space:pre-wrap;"><code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">or</span> like</code>、<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">not</span> like</code>、<code class="hljs coffeescript" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">or</span><span class="hljs-keyword" style="color:rgb(167,29,93);">not</span> like</code>用法和<code class="hljs" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;">like</code>一样。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;or like&#39;, &#39;name&#39;, [&#39;test&#39;, &#39;sample&#39;]] // 生成:name LIKE &#39;%test%&#39; OR name LIKE &#39;%sample%&#39;[&#39;not like&#39;, &#39;name&#39;, &#39;tester&#39;] // 生成:name NOT LIKE &#39;%tester%&#39;[&#39;or not like&#39;, &#39;name&#39;, [&#39;test&#39;, &#39;sample&#39;]] // 生成:name NOT LIKE &#39;%test%&#39; OR name NOT LIKE &#39;%sample%&#39;</pre><div class="contentsignin">登入後複製</div></div><h3 id="exists">3.8 exists</h3><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>[&#39;exists&#39;, (new Query())->select(&#39;id&#39;)->from(&#39;users&#39;)->where([&#39;active&#39; => 1])] // 生成:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)</pre><div class="contentsignin">登入後複製</div></div></p> <p style="white-space:pre-wrap;"><code class="hljs perl" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">not</span><span class="hljs-keyword" style="color:rgb(167,29,93);">exists</span></code>用法和<code class="hljs perl" style="font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:12px;padding:.3em;color:rgb(51,51,51);background-color:rgb(247,247,247);display:inline;line-height:1.65;"><span class="hljs-keyword" style="color:rgb(167,29,93);">exists</span></code>一样。</p> <p style="white-space: pre-wrap;"> 相关推荐:</p> <p style="white-space: pre-wrap;"><a href="http://www.php.cn/php-weizijiaocheng-330818.html" target="_self">Yii20中文开发向导——Where条件查询全解析</a></p>

以上是Yii2查詢where條件組裝詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

yii2 怎麼去掉jquery yii2 怎麼去掉jquery Feb 17, 2023 am 09:55 AM

yii2去掉jquery的方法:1、編輯AppAsset.php文件,註解掉變數$depends裡的「yii\web\YiiAsset」值;2、編輯main.php文件,在欄位「components」下方新增配置為「'yii \web\JqueryAsset' => ['js' => [],'sourcePath' => null,],」即可去掉jquery腳本。

Laravel 集合中的 Where 方法實用指南 Laravel 集合中的 Where 方法實用指南 Mar 10, 2024 pm 04:36 PM

Laravel集合中的Where方法實用指南在Laravel框架的開發過程中,集合(Collection)是一個非常有用的資料結構,它提供了豐富的方法來操作資料。其中,Where方法是常用的篩選方法,能夠根據指定條件來過濾集合中的元素。本文將介紹Laravel集合中Where方法的使用,透過具體的程式碼範例來示範其用法。 1.基本用法Where方法的

Laravel 集合如何使用 Where 方法 Laravel 集合如何使用 Where 方法 Mar 10, 2024 pm 10:21 PM

Laravel集合中如何使用Where方法Laravel是一個流行的PHP框架,它提供了豐富的功能和工具,方便開發者快速建立應用程式。其中,集合(Collection)是Laravel中一個非常實用且強大的資料結構,開發者可以使用集合對資料進行各種操作,例如過濾、映射、排序等。在集合中,Where方法是一個常用的方法,用於根據指定的條件過濾集

從入門到精通:掌握is與where選擇器的使用技巧 從入門到精通:掌握is與where選擇器的使用技巧 Sep 08, 2023 am 09:15 AM

從入門到精通:掌握is與where選擇器的使用技巧引言:在進行資料處理與分析的過程中,選擇器(selector)是一項非常重要的工具。透過選擇器,我們可以按照特定的條件從資料集中提取所需的資料。本文將介紹is和where選擇器的使用技巧,幫助讀者快速掌握這兩個選擇器的強大功能。一、is選擇器的使用is選擇器是一種基本的選擇器,它允許我們根據給定條件對資料集進

精選幾道CTF練習,帶你學習yii2框架! 精選幾道CTF練習,帶你學習yii2框架! Feb 23, 2022 am 10:33 AM

本篇文章帶大家了解yii2框架,分享幾個CTF習題,透過它們來學習yii2框架,希望對大家有幫助。

mysql left join的基本用法及on與where的差別是什麼 mysql left join的基本用法及on與where的差別是什麼 Jun 02, 2023 pm 11:54 PM

前言我們在寫sql語句的時候,總是無法避免使用到連接關鍵字,例如內連接、外連接。種類是很多的,我在這裡貼上一張在別處找到的圖:這張圖我認為是非常詳細了,它展示出了SQL語句中常見的鏈接類型,以本文中的leftjoin為例,網上是這麼給定義的:LEFTJOIN關鍵字會從左表傳回所有的行,即使在右表中沒有符合的行。其實光從字面意思上來說的話,leftjoin是比較好理解的,但是在使用的過程中,還是會有一些問題的,比如條件在on後面與在where後面,他們的結果是完全不一樣的,接下來我們就從淺到深

怎麼使用YII2框架安裝Redis擴展 怎麼使用YII2框架安裝Redis擴展 May 26, 2023 pm 06:41 PM

1.需要下載yii2-redis的master分支windows版本composer下載2.解壓縮複製到vendor/yiisoft下面3.yiisoft下面extensions.php裡面增加'yiisoft/yii2-redis'=>array('name'=>'yiisoft /yii2-redis','version'=>'2.0.

Laravel 集合中的 Where 方法用法解析 Laravel 集合中的 Where 方法用法解析 Mar 09, 2024 pm 06:51 PM

Laravel是一款受歡迎的PHP開發框架,它提供了豐富且便利的功能,其中集合(Collection)是Laravel中非常重要的資料結構之一。集合類別提供了許多強大的方法,其中一個常用的方法是where方法。本文將透過具體的程式碼範例來解析Laravel集合中的where方法用法。 1.建立集合首先,我們需要建立一個包含一些資料的集合。可以

See all articles