Mysql 在带有左连接的 codeigniter 中不存在

问题描述 投票:0回答:1

我有2张桌子

tablea
id_a | name_a
-------------
  1  | one
  2  | two
  3  | three
  4  | four
  5  | five
  6  | six

tableb
id_b | name_b
-------------
  1  | a
  2  | b

Ì想要的结果是

id_a | id_b | name_b
----------------------------
  1  |  1   |  a
  2  |  2   |  b
  3  | null | null
  4  | null | null
  5  | null | null
  6  | null | null

在那个表中我想转换这个 mysql 查询

SELECT * FROM tablea
WHERE NOT EXISTS (SELECT * FROM tableb WHERE tableb.`id_b`=tablea.`id_a`)

进入 codeigniter 查询

我试过这个查询但没有用

$this->db->join('tableb', 'tableb.id_ab=tablea.id_a','left'); 
$q = $this->db->get_where('tabela',array('tableb.id_b !='=> 4));
return $q->result_array();

我举个例子,找一个不为4的id(id_b),当然不在表b中,结果为null

请帮助我。 非常感谢

mysql codeigniter-3 not-exists
1个回答
0
投票

您也可以使用 CodeIgniter 数据库查询。

$this->db->query('
     SELECT tableA.id_a,
            tableB.id_b,
            tableB.name_b
     FROM tableA
     LEFT JOIN tableB ON tableB.id_b = tableA.id_a
     WHERE tableB.id_b != "4" 
     OR tableB.id_b IS NULL)
');
© www.soinside.com 2019 - 2024. All rights reserved.