Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get non-matching records from two tables

Tags:

mysql

See these two sample tables:

Table 1:

id    acc_no    name
------------------------
1     14        aaaa
2     16        bbbb
3     18        ccccc
4     25        wwww
5     27        xxxxxxx
6     28        zzzzzzz

Table 2:

sr no   acc_no  amount
----------------------
1       14      2000
2       16      2344
3       18      3200

I need to get records on basis of acc_no which are not matching in table 1 for example:

OUTPUT:

id   acc_no   name
---------------------
4    25       wwww
5    27       xxxxxxx
6    28       zzzzzzz

When I tried with below query ,the result was not reliable:

SELECT t1.* 
FROM table1 t1
     LEFT OUTER JOIN table2 t2 ON t1.acc_no = t2.acc_no
WHERE t2.acc_no IS NULL

Give your suggestions. What will be right SQL query ti get above output?

like image 892
user1829627 Avatar asked Feb 12 '13 07:02

user1829627


People also ask

How do you find non matching values in SQL?

To find rows that do not match a value, use the NOT operator.

Which join is used to take the unmatched data from 2 tables?

FULL OUTER JOIN returns unmatched rows from both tables,as well as matched rows in both tables.


1 Answers

try:

SELECT * 
FROM table1 t1
WHERE t1.acc_no NOT IN (SELECT acc_no FROM table2)
like image 154
Euclides Mulémbwè Avatar answered Oct 01 '22 14:10

Euclides Mulémbwè