如何选择不同的记录?

时间:2016-01-13 14:41:24

标签: sqlite

我有一个列number的SQLite表可能有重复项。

如何选择按编号获取不同行的记录?

SELECT 
    contacts_table._id AS _id, 
    title, title_short,  
    DISTINCT number, 
    note, 
    categories, 
    contacts_categories_name, 
    contact_id, 
    contacts_categories_catalog_id 
FROM contacts_table 
LEFT OUTER JOIN contacts_categories_table 
    ON(contacts_categories_contact_id = contact_id 
        AND contacts_categories_current_user = current_user)  
WHERE ((current_user = ? AND saved =? 
    AND ( title LIKE ? OR  number LIKE ? OR note LIKE ? )) 
    AND (contacts_categories_catalog_id = ? )) 
ORDER BY 
    contacts_categories_name = 'UNSORTED' DESC,
    contacts_categories_name ASC, 
    title ASC

1 个答案:

答案 0 :(得分:0)

只需添加GROUP BY:

SELECT 
    MIN(contacts_table._id) AS _id,
    title,
    title_short,
    number,
    note,
    categories,
    contacts_categories_name,
    contact_id,
    MIN(contacts_categories_catalog_id)
FROM ...
GROUP BY
    number
ORDER BY ...;