I have two table in my DB as table tbl_a and tbl_b which are having following data.
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
How can I get the tbl_b.a_name values which are not present in the tbl_a table as a_name.
My desire output should be like,
Joeees
Joeeess
Right now I am having the following code.
$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;
}
Thank in advance.
The simplest way is to use not exists
:
select a.*
from tbl_a a
where not exists (select 1 from tbl_b b where b.b_name = a.a_name);
Try this
SELECT
DISTINCT tbl_b.a_name
FROM tbl_b where tbl_b.a_name NOT IN (SELECT DISTINCT tbl_a.a_name from tbl_a)
I got the solution thanks for the help anyways..
SELECT
tbl_b.a_name
FROM tbl_b
WHERE tbl_b.u_id='1'
AND NOT EXISTS (SELECT * FROM tbl_a WHERE tbl_a.u_id='1' AND tbl_b.a_name = tbl_a.a_name)
You can try not in
same as follow:
SELECT * FROM `tbl_b`
where a_name not in (select DISTINCT a_name from tbl_a)
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
You can try this it may be useful and execute fast then other query.