Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何简化此查询?_Php_Codeigniter - Fatal编程技术网

Php 如何简化此查询?

Php 如何简化此查询?,php,codeigniter,Php,Codeigniter,如果查看以下查询: $query = $this->db ->select('*') ->from('sets') ->where('type', 'd') ->or_where('type', 'e') ->or_where('type', 'f') ->or_where('type', 'g') ->or_where('type',

如果查看以下查询:

$query = $this->db
        ->select('*')
        ->from('sets')
        ->where('type', 'd')
        ->or_where('type', 'e')
        ->or_where('type', 'f')
        ->or_where('type', 'g')
        ->or_where('type', 'h')
        ->or_where('type', 'i')
        ->or_where('type', 'j')
        ->or_where('type', 'k')
        ->or_where('type', 'l')
        ->or_where('type', 'm')

                    ...

        ->or_where('type', 'x')
        ->or_where('type', 'y')
        ->or_where('type', 'z')
        ->order_by('date', 'asc');

    return $query->get()->result();

在'type'列中,我有'a'到'z',基本上我希望所有行减去带a,b,c的行。如何以更优雅的方式编写此命令?

因为您基本上需要除a、b或c之外的所有行,所以可以使用where\u not\u in指令:

select * from sets where type not in ('a','b','c')
$query = $this->db
    ->select('*')
    ->from('sets')
    ->where_not_in('type', array('a','b','c'))
    ->order_by('date', 'asc');
例如,请参见当心数组

使现代化 为了增加趣味性,使用where_in还是where_not_in实际上并不那么简单,只需使用生成最短/最简单SQL的方法即可。如果您有一个大表,并且要查找的集合的基数很大,并且字段上有一个索引,那么根据许多因素,进行负搜索的效率可能远远低于明显较大的正搜索

索引善于寻找其中的内容,而不是不在其中的内容。因此,您可能会发现这样做是有益的:

->where_in('type', array_diff(  // Where type is...
    range('a', 'z'),            // ...everything...
    array('a','b','c')          // EXCEPT a, b or c
))

// The above solution works also if you look for non-consecutive letters.
然后MySQL可能会进行二十次非常快速的索引查找d、e、f,…,而不是在可能较慢的全表扫描上只进行三次比较。根据您要查找的项目数量、索引、磁盘性能、表大小以及我是否忽略了天气?由于天气原因,即使SQL更复杂,第二种方法也可能更快


当您处于查询优化阶段时,这肯定是值得关注的。不要像一些在线所谓教程中所建议的那个样去索引每一列——索引有时会对性能有害,而且根据定义过度索引总是有害的。

制作一个从d到z的字符数组,如:

$chars = array('d', 'e'...., 'z');
现在,使用where_in子句如下:

$rs = $this->db->select('')->where_in('type', $chars)->get('table');

也许是这样的

$query = $this->db
        ->select('*')
        ->from('sets')

        ->where_in('type', range('d','z'))

        ->order_by('date', 'asc');

    return $query->get()->result();
这应该行得通


还请注意,在选择所有内容并使用GET时,不需要使用SELECT或FROM。

不确定CI的DBAL是否支持它,但在mysql中,两者之间存在。否则,您可以使用IN和PHP range函数result的内爆来创建它。在这里,\u IN代替+1,您可以使用SELECT*FROM foo where NOT exists从foo中选择1,其中键入a、b、c,这会将负搜索转化为正搜索。
$this->db->where('type !=', 'a');
$this->db->where('type !=', 'b');
$this->db->where('type !=', 'c');
$this->db->order_by('date', 'asc');
$query = $this->db->get('sets');