• <legend id='tmAPS'><style id='tmAPS'><dir id='tmAPS'><q id='tmAPS'></q></dir></style></legend>

    <small id='tmAPS'></small><noframes id='tmAPS'>

  • <i id='tmAPS'><tr id='tmAPS'><dt id='tmAPS'><q id='tmAPS'><span id='tmAPS'><b id='tmAPS'><form id='tmAPS'><ins id='tmAPS'></ins><ul id='tmAPS'></ul><sub id='tmAPS'></sub></form><legend id='tmAPS'></legend><bdo id='tmAPS'><pre id='tmAPS'><center id='tmAPS'></center></pre></bdo></b><th id='tmAPS'></th></span></q></dt></tr></i><div id='tmAPS'><tfoot id='tmAPS'></tfoot><dl id='tmAPS'><fieldset id='tmAPS'></fieldset></dl></div>

        <tfoot id='tmAPS'></tfoot>
        • <bdo id='tmAPS'></bdo><ul id='tmAPS'></ul>

        动态 Active Record 查询中的 Codeigniter 括号

        时间:2023-08-19

                <tbody id='vRgAS'></tbody>

              <small id='vRgAS'></small><noframes id='vRgAS'>

            1. <tfoot id='vRgAS'></tfoot>
            2. <legend id='vRgAS'><style id='vRgAS'><dir id='vRgAS'><q id='vRgAS'></q></dir></style></legend>
            3. <i id='vRgAS'><tr id='vRgAS'><dt id='vRgAS'><q id='vRgAS'><span id='vRgAS'><b id='vRgAS'><form id='vRgAS'><ins id='vRgAS'></ins><ul id='vRgAS'></ul><sub id='vRgAS'></sub></form><legend id='vRgAS'></legend><bdo id='vRgAS'><pre id='vRgAS'><center id='vRgAS'></center></pre></bdo></b><th id='vRgAS'></th></span></q></dt></tr></i><div id='vRgAS'><tfoot id='vRgAS'></tfoot><dl id='vRgAS'><fieldset id='vRgAS'></fieldset></dl></div>

                <bdo id='vRgAS'></bdo><ul id='vRgAS'></ul>

                  本文介绍了动态 Active Record 查询中的 Codeigniter 括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 ActiveRecord 生成如下查询

                  I'm producing a query like the following using ActiveRecord

                  SELECT * FROM (`foods`) WHERE `type` = 'fruits' AND 
                         `tags` LIKE '%green%' OR `tags` LIKE '%blue%' OR `tags` LIKE '%red%'
                  

                  tags 的数量和值未知.数组是动态创建的.下面我添加了一个可能的数组.

                  The number of tags and values is unknown. Arrays are created dynamically. Below I added a possible array.

                  $tags = array (                 
                          '0'     => 'green'.
                          '1'     => 'blue',
                          '2'     => 'red'
                  );  
                  

                  有一个标签数组,我使用以下循环来创建我在上面发布的查询.

                  Having an array of tags, I use the following loop to create the query I posted on top.

                  $this->db->where('type', $type); //var type is retrieved from input value
                  
                  foreach($tags as $tag):         
                       $this->db->or_like('tags', $tag);
                  endforeach; 
                  

                  问题:我需要在 LIKE 子句周围添加括号,如下所示:

                  The issue: I need to add parentheses around the LIKE clauses like below:

                  SELECT * FROM (`foods`) WHERE `type` = 'fruits' AND 
                        (`tags` LIKE '%green%' OR `tags` LIKE '%blue%' OR `tags` LIKE '%red%')
                  

                  如果括号内的内容是静态的,但 foreach 循环让我失望,我知道如何完成此操作..

                  I know how to accomplish this if the content within the parentheses was static but the foreach loop throws me off..

                  推荐答案

                  来自 CI wiki:

                  From the CI wiki:

                  codeignighter ActiveRecord 功能允许您创建 SQL 查询相对简单和独立于数据库,但是有没有特别支持包括SQL 查询中的括号.

                  The codeignighter ActiveRecord feature allows you to create SQL queries relatively simply and database-independant, however there isno specific support for including parenthesis in an SQL query.

                  例如,当您希望 where 语句与以下内容类似时:

                  For example when you want a where statement to come out simmilarly to the folowing:

                  WHERE (field1 = value || field2 = value) AND (field3 = value2 || field4 = value2) 
                  

                  这可以通过向 CI->db->where() 函数提供字符串来解决,在这种情况下,您需要专门转义您的值.

                  This can be worked around by feeding a string to the CI->db->where() function, in this case you will want to specifically escape your values.

                  看下面的例子:

                  $value=$this->db->escape($value);
                  $value2=$this->db->escape($value2);
                  $this->db->from('sometable');
                  $this->db->where("($field = $value || $field2 = $value)");
                  $this->db->where("($field3 = $value2 || $field4 = $value2)");
                  $this->db->get(); 
                  

                  类似的解决方法可用于 LIKE 子句:

                  A simmilar workaround can be used for LIKE clauses:

                  $this->db->where("($field LIKE '%$value%' || $field2 LIKE '%$value%')");
                  $this->db->where("($field3 LIKE '%$value2%' || $field4 LIKE '%$value2%')"); 
                  

                  这篇关于动态 Active Record 查询中的 Codeigniter 括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL按数组值排序 下一篇:在sql server中保存字节数组

                  相关文章

                  最新文章

                    <bdo id='oo9sB'></bdo><ul id='oo9sB'></ul>

                    <tfoot id='oo9sB'></tfoot>
                  1. <small id='oo9sB'></small><noframes id='oo9sB'>

                    <i id='oo9sB'><tr id='oo9sB'><dt id='oo9sB'><q id='oo9sB'><span id='oo9sB'><b id='oo9sB'><form id='oo9sB'><ins id='oo9sB'></ins><ul id='oo9sB'></ul><sub id='oo9sB'></sub></form><legend id='oo9sB'></legend><bdo id='oo9sB'><pre id='oo9sB'><center id='oo9sB'></center></pre></bdo></b><th id='oo9sB'></th></span></q></dt></tr></i><div id='oo9sB'><tfoot id='oo9sB'></tfoot><dl id='oo9sB'><fieldset id='oo9sB'></fieldset></dl></div>
                    <legend id='oo9sB'><style id='oo9sB'><dir id='oo9sB'><q id='oo9sB'></q></dir></style></legend>