Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two tables in SQLite

Tags:

sql

sqlite

I have two tables and want to compare rows on sqlite like this

table1           table2
field1           field1

a                   a
b                   d
c                   f
d                   g
e
f
g
h
i

and I want to produce result like this

result_table
field1

b
c
e
h
i

How is the syntax in sqlite? Thanks

like image 876
kuslahne Avatar asked Jan 12 '12 07:01

kuslahne


People also ask

How can I compare two SQLite databases?

Basically - in order to compare two SQLite database files, you need to click the "Compare..." button. This will open up the "Comparison Details" dialog in which you'll fill in the paths to both SQLite database files and choose the comparison mode: Compare schema only- For comparing only SQL schema differences.

How do you compare two columns values in two different tables in SQL?

Using joins to compare columns by priority among the table. For example, left join returns all values from the first table and null value for the not-matched records from the second table. Similarly, we can use right join, inner join, full join and self join as per our requirements.


2 Answers

SELECT DISTINCT Field1
FROM Table1 
WHERE Field1 Not IN 
    (SELECT DISTINCT Field1 FROM Table2)
like image 177
John Woo Avatar answered Oct 06 '22 09:10

John Woo


SELECT columns1 FROM table1 EXCEPT SELECT columns2 FROM table2;

The SQLite EXCEPT clause returns all rows from the left SELECT statement that are not in the result of the second SELECT statement. The number of columns selected must be the same in both SELECT statements.

This works fine for small to medium size tables. Avoid for tables with millions of lines.

See Compound Select Statements and the documentation of the SQLite SELECT statement.

like image 23
Aurel Wisse Avatar answered Oct 06 '22 11:10

Aurel Wisse