简体   繁体   English

Codeigniter Active Record查询顺序

[英]Codeigniter Active Record query order

What's the difference between this query: 此查询之间有什么区别:

$this->db->select('*');
$this->db->from('logs AS l');
$this->db->join('log_group_ref AS r', 'l.log_id = r.log_id');
$this->db->like('l.log_title', 'hello');
$this->db->or_like('l.log_content', 'hello');
$this->db->where('r.group_id', 1);

and this: 和这个:

SELECT * FROM logs as l
join log_group_ref as r
on l.log_id = r.log_id
where l.log_title like "%hello%"
or l.log_content like "%hello%"
and r.group_id = 1

When I run the CI Active record way, I get different result against the generic query. 当我运行CI Active记录方式时,针对通用查询会得到不同的结果。

When I intentionally put error on the CI query, like removing a letter from a field to see the error on the console, it moves the WHERE clause after the JOIN clause. 当我有意在CI查询中放入错误时,例如从字段中删除一个字母以在控制台上看到错误,它会将WHERE子句移到JOIN子句之后。

And I think it makes a difference because I receive different results from both. 我认为这有所作为,因为我从两者获得不同的结果。

Any idea how to fix this? 任何想法如何解决这个问题?

Or I'm running a wrong query? 还是我运行了错误的查询?

Update: 更新:

As requested, here's the CI generated query: 根据要求,这是CI生成的查询:

Error Number: 1054

Unknown column 'r.group_i' in 'where clause'

SELECT `*` FROM (`logs` AS l)JOIN `log_group_ref` AS r ON 
`l`.`log_id` = `r`.`log_id`JOIN `users` AS u ON `u`.`user_id`=`l`.`log_author`WHERE 
`r`.`group_i` = '1' AND `l`.`log_title` LIKE '%at%'OR `l`.`log_content` LIKE '%at%'

Filename: C:\xampp\htdocs\done\system\database\DB_driver.php

Line Number: 330

Here, I intentionally remove the 'd' in r.group_id in the last WHERE clause. 在这里,我有意在最后一个WHERE子句中删除了r.group_id中的'd'。

You have a diference because its a difererece if you selecting 您与众不同,因为如果您选择

( someting like '%a'% or someting like '%b' ) and someting = 1
someting like '%a'% or ( someting like '%b'  and someting = 1)
someting like '%a'% or  someting like '%b'  and someting = 1

so there is the diference as i wrote in the comment use 所以有我在评论中写的区别

$this->output->enable_profiler(TRUE);

to se what CI did you will se queries and the end of the page and whole lot of oder data but to use ( ) inside CI you need someting like 要确定您要查询的CI,以及页面末尾以及大量其他数据,但是要在CI中使用(),您需要

$this->db->where('(someting like "%'.$a.'%" or someting like "%'.$b.'%" )',null,false)

i hope it helps 我希望它会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM