views:

69

answers:

3

hey can some tell me the query in sqlite3 which can be used to select the entry only once if it exists more than once in my database

+1  A: 

From the manual:

The DISTINCT keyword causes a subset of result rows to be returned, in which each result row is different. NULL values are not treated as distinct from each other.

In short, SELECT DISTINCT ... eliminates duplicate rows.

Dan LaRocque
+1  A: 
Select DISTINCT <Column> FROM <Table> order by <Column>
Salil
A: 

You can also use GROUP BY:

  SELECT <Column> 
    FROM <Table> 
GROUP BY <Column>

E.g.:

>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> c = conn.cursor()
>>> c.execute("CREATE TABLE test (col text);")
<sqlite3.Cursor object at 0x7f9ba3ec3c90>
>>> c.execute("INSERT INTO test VALUES ('distinct');")
<sqlite3.Cursor object at 0x7f9ba3ec3c90>
>>> c.execute("INSERT INTO test VALUES ('distinct');")
<sqlite3.Cursor object at 0x7f9ba3ec3c90>
>>> c.execute("SELECT col FROM test;").fetchall()              # three results
[(u'distinct',), (u'distinct',), (u'distinct',)] 
>>> c.execute("SELECT col FROM test GROUP BY col;").fetchall() # one result
[(u'distinct',)]
Adam Bernier