简体   繁体   English

SQL加入Codeigniter吗?

[英]Sql Join in Codeigniter?

I want to convert this query to codeigniter, but the last line - 'not in' is not working (I want the tests which are not there in assigned table) 我想将此查询转换为codeigniter,但最后一行-'not in'不起作用(我希望分配表中不存在的测试)

SELECT * FROM (`tests` AS A) 
INNER JOIN `test_types` AS B ON `A`.`tst_test_type_id` = `B`.`tt_id` 
INNER JOIN `app_types` AS C ON `A`.`tst_app_type_id` = `C`.`at_id`
 AND tst_id not in (select ast_test_id from assigned_tests)

what so far I tried 到目前为止我尝试了什么

  $this->db->select('*');
    $this->db->from('tests AS A');// I use aliasing make joins easier
    $this->db->join('test_types AS B', 'A.tst_test_type_id = B.tt_id', 'INNER');
    $this->db->join('app_types AS C', 'A.tst_app_type_id = C.at_id', 'INNER');
    $this->db->join('assigned_tests AS D', 'A.tst_id = D.ast_test_id', 'outer');

from the question you are saying "converting this query to codeigniter" but that's not true, you should have said "converting to active record", in fact codeigniter can use active record class or it can use native queries too, and they use less resources than active record I quote from the doc 从您说“将查询转换为codeigniter”的问题出发,但那不是真的,您应该说“转换为活动记录”,实际上codeigniter可以使用活动记录类,也可以使用本机查询,并且它们使用的资源更少比我从文档引用的活动记录

Note: If you intend to write your own queries you can disable this class (Active Record) in your database config file, allowing the core database library and adapter to utilize fewer resources. 注意:如果要编写自己的查询,则可以在数据库配置文件中禁用此类(活动记录),从而允许核心数据库库和适配器使用较少的资源。

so to make it short, you can use your query like so : 为了简短起见,您可以像这样使用查询:

$qry = $this->db->query('SELECT * FROM (`tests` AS A) 
INNER JOIN `test_types` AS B ON `A`.`tst_test_type_id` = `B`.`tt_id` 
INNER JOIN `app_types` AS C ON `A`.`tst_app_type_id` = `C`.`at_id`
AND tst_id not in (select ast_test_id from assigned_tests)');

return $qry->result();

You can submit the sub-query as a parameter by setting the escape flag to false . 通过将转义标志设置为false可以将子查询作为参数提交。

$this->db->select('*');
$this->db->from('tests AS A');
$this->db->join('test_types AS B', 'A.tst_test_type_id = B.tt_id', 'INNER');
$this->db->join('app_types AS C', 'A.tst_app_type_id = C.at_id', 'INNER');
$this->db->join('assigned_tests AS D', 'A.tst_id = D.ast_test_id', 'outer');
$this->db->where('tst_id not in', '(select ast_test_id from assigned_tests)', false);

i found this working with IS NULL - pl post if some thing extra ordinary is there 我发现这与IS NULL一起使用-如果存在一些超常规的事情,请张贴

$this->db->select('*');
        $this->db->from('tests AS A');
        $this->db->join('test_types AS B', 'A.tst_test_type_id = B.tt_id', 'INNER');
        $this->db->join('app_types AS C', 'A.tst_app_type_id = C.at_id', 'INNER');
        $this->db->join('assigned_tests AS D', 'A.tst_id = D.ast_test_id', 'left');
        $this->db->where('D.ast_test_id IS NULL');

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

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