problem with sqlite database query, the distinct query not working
in my sqlite database, a table named image contains three fields label, url and index. I wrote the following piece of code for fetching data from database: "SELECT DISTINCT(label), index from image;". In my table there is a label 'Cat' 3 times. According to this code the code must show only one 'Cat开发者_运维百科' from my database. But it won't working. It fetches all three 'Cat' label. Why it happens? please help me to find a possible solution. index field is different for all three 'Cat' labels.
The DISTINCT keyword is not a function, it specifies that duplicate rows should be removed from the results:
If the simple SELECT is a SELECT DISTINCT, then duplicate rows are removed from the set of result rows before it is returned
What you are trying to accomplish probably requires you to group by label:
SELECT label, index FROM image GROUP BY label
Try this:
select label, index from image
where label in (select distinct label from image)
精彩评论