Mysql两个表,获取没有相同值的数据

时间:2015-12-09 03:11:05

标签: php mysql codeigniter

我的数据库中有两个表作为表tbl_a和tbl_b,它们有以下数据。

tbl_a

 tbl_a_id  u_id  a_name
     1      1      Joe
     2      1      Joel
     3      1      Joele
     4      1      Joelle
     5      3      Joeee

tbl_b

 tbl_b_id  u_id  a_name
     1      1      Joe
     2      1      Joel
     3      1      Joele
     4      1      Joelle
     5      3      Joeee
     5      1      Joeees
     5      1      Joeeess

如何将 tbl_a 表中不存在的 tbl_b.a_name 值作为a_name。

我的愿望输出应该是,

Joeees

Joeeess

现在我有以下代码。

 $qqq = $this->db->query("
    SELECT 
        tbl_b.a_name
    FROM tbl_b
    WHERE tbl.u_id='1'
 ");

  foreach($qqq->result() as $ggg)
  {
     echo $ggg->a_name;
  }

提前感谢。

5 个答案:

答案 0 :(得分:2)

最简单的方法是使用not exists

select a.*
from tbl_a a
where not exists (select 1 from tbl_b b where b.b_name = a.a_name);

答案 1 :(得分:0)

我得到了解决方案感谢你的帮助..

transform= "rotate (<?php echo rand(min,max); ?> deg);"// or simply rand()

答案 2 :(得分:0)

您可以尝试not in,如下所示:

SELECT * FROM `tbl_b`
where a_name not in (select DISTINCT a_name from tbl_a)

答案 3 :(得分:0)

select a.*,b.*
from tbl_a a left join tbl_b b on a.u_id = b.u_id
where b.b_name != a.a_name

你可以试试这个可能很有用,然后快速执行其他查询。

答案 4 :(得分:-1)

试试这个

SLN