Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select unmatched records from two tables of MYSQL

Tags:

sql

php

mysql

I have two tables Table1 and Table2 with some records

id is the common column in both tables and primarykey is set to this column in table1

There are many records in table1 and some of these records (not all) are updated into table2.

Now I want retrieve from table1 the records not updated into the table2.

For example in table1 there are records 1,2,3,4,5,6,7,8,9

And in table2 there are 3,4,7,9

Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2

I wrote this query :

SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;

But the expected result not coming. This query lists all the records repeating each one record manytimes

Can any body give me solution to get the expected result.

like image 602
user1065055 Avatar asked Nov 25 '11 07:11

user1065055


1 Answers

select * 
from table1
where table1.slip_no NOT IN (select id from table2)

Assuming name of common column is id

Or you can modify your query as

SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1] 
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
like image 64
Zohaib Avatar answered Sep 25 '22 08:09

Zohaib