user391301
user391301

Reputation: 21

select query in sqlite

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

Upvotes: 0

Views: 481

Answers (3)

mechanical_meat
mechanical_meat

Reputation: 169494

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',)]

Upvotes: 0

Salil
Salil

Reputation: 47532

Select DISTINCT <Column> FROM <Table> order by <Column>

Upvotes: 1

Dan LaRocque
Dan LaRocque

Reputation: 5183

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.

Upvotes: 1

Related Questions