如何在SQL中的非键上运行DISTINCT

时间:2011-09-11 12:26:07

标签: android sql sqlite

我有一个数据库可以有类似的行,但有不同的键和不同的布尔列。这是数据库的样子:

列:_id,name,img,address,available

两个条目可能如下所示:

_id | name | img                | address   | available
-------------------------------------------------------
1   | John | http://img.com/222 | 1 Main St | 1
2   | John | http://img.com/222 | 1 Main St | 0

我想要一个查询,它会给我所有具有不同键的结果,如果有重复的条目(忽略_id会有所不同的事实),它将只返回第一个。这是我的查询:

SELECT p1.* 
FROM (SELECT DISTINCT _id, available FROM people) p
    INNER JOIN people p1
    ON p1._id=p._id
ORDER BY p1.available DESC;

我知道这不对,但也许它解释了我正在寻找的东西。我想在这里使用GROUP BY吗?

4 个答案:

答案 0 :(得分:1)

I want a query that will give me all results that have a distinct key, and if there are duplicate entries(ignoring the fact that _id would be different), it will give back only the first one.....the _id isn't what I want to be distinct, as they [the ids] are already unique. ... . Ideally it will order by 'available' in descending order so that if there are two columns with the same data(aside from _id and available), it will return the row with '1' for the available column

             select name, image, address, max(availability) as avail
              from T
             group by name, image, address

然后,您可以将上面查询返回的集合作为内联视图加入到您的表中:

            select * from T
            inner join
            (

             select name, image, address, max(availability) avail
              from T
             group by name, image, address
            ) as foo

            on T.name = foo.name and T.image = foo.image and T.address = foo.address and T.availability = foo.avail

如果有一个复合索引会有所帮助:(名称,图像,地址)。

警告:如果特定{name,image,address}三元组的可用性为1的行有多行,则查询将为三元组返回多行:

             2   | John | http://img.com/222 | 1 Main St | 1
             6   | John | http://img.com/222 | 1 Main St | 1

P.S。听起来好像你希望在你的表中创建三元组(名称,图像,地址)一个备用的UNIQUE键。

答案 1 :(得分:0)

这个sql可以解决你的问题:

选择b。* from(从人们选择不同的_id)a,人们b其中a._id = b._id按b.available排序

答案 2 :(得分:0)

我实际上刚刚问了一个类似的问题,并从一位经验丰富的用户那里得到了很好的答案:

SQL Populating with distinct data and a sequence

根据他告诉我的内容,这个查询可能会为您提供所需内容:

SELECT p1.* 
FROM (SELECT DISTINCT _id, name from people) p
   INNER JOIN people p1
   ON p1._id=p._id
ORDER BY p1.available desc
如果失败并且不起作用,请道歉!

编辑:我刚想到我不知道哪个名称+ _id组合这将提取..可用= 1或者可用= 0或随机选择..!让我知道无论如何会发生什么......

答案 3 :(得分:0)

如果你想要_idavailable值最低的_id的第一行,那么你可以“记录”available里面的_id分组生成的汇总值。

要比较的值的构建方式是按照_id字段的降序排列记录,然后按降序排列select people.* from people inner join ( select name, img, address, min((1-available)*100000000 + _id) avail_id from people group by name, img, address ) as foo on people._id = foo.avail_id % 100000000; 字段,并允许轻松检索select people.* from people left outer join people as other on other.name = people.name and other.img = people.img and people.address=other.address and (1 - people.available) * 100000000 + people._id > (1 - other.available) * 100000000 + other._id where other.available is null; 使用模运算符(假设可用的最大值为1,且id永远不会超过100000000)。

{{1}}

我改编了Tim's query

您也可以执行without subquery

{{1}}