【问题标题】:Codeigniter Active Record query orderCodeigniter Active Record 查询顺序
【发布时间】:2013-02-22 03:54:14
【问题描述】:

这个查询有什么区别:

$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);

还有这个:

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

当我运行 CI 活动记录方式时,我会得到与通用查询不同的结果。

当我故意将错误放在 CI 查询上时,例如从字段中删除一个字母以在控制台上查看错误,它会将 WHERE 子句移到 JOIN 子句之后。

我认为这会有所不同,因为我从两者得到的结果不同。

知道如何解决这个问题吗?

或者我运行了一个错误的查询?

更新:

根据要求,这是 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

这里,我特意删除了最后一个 WHERE 子句中 r.group_id 中的 'd'。

【问题讨论】:

  • 将 CI 生成的查询粘贴到问题中以查看差异
  • $this->output->enable_profiler(TRUE);你会看到你运行的所有查询

标签: mysql codeigniter


【解决方案1】:

你有一个不同,因为如果你选择它会有所不同

( 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

所以有区别 正如我在评论中写的那样使用

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

要查看 CI 做了什么,您将查看查询、页面末尾和大量 oder 数据 但要在 CI 中使用 (),您需要类似

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2012-04-17
    • 2016-03-15
    • 2013-01-09
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    相关资源
    最近更新 更多