0

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

3 Answers 3

1
Select DISTINCT <Column> FROM <Table> order by <Column>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.