SQL - 根据不同的属性获取任何行,不重复 [英] SQL - Fetch any rows based on a distinct property, no duplicates
问题描述
是否有可能运行一个查询,该查询表示为该表提取所有具有不同属性的表,并为该不同属性选择任何匹配的行,这对于基于一个属性的唯一/不同行无关紧要。
如果
row1:name:a,age:10,街道:james street
row2:name:b,age:15,street:peter street
row3:name:c,age:17,street:james street
现在我希望查询返回
row2和row1或row2
无论是row1还是row2,不在乎。
现在,理想情况下我想使用hibernate标准查询来执行此操作,因为我已经有了一个查询生成器,并且我只是将其添加为一个选项。
您可以尝试投影: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html#querycriteria-预测
类似这样的工作可能会奏效。它将根据街道属性选择唯一的结果:
列出结果= session.createCriteria(Entity.class)
.setProjection(Projections.projectionList()
.add(Projections.distinct(Projections.property(street)))
)
.list();
Is it possible to run a query, that says fetch all for this table that have distinct property and select any matched row for that distinct property, doesn't matter to unique/distinct rows based on just one property.
If
row1 : name:a, age:10, street:james street
row2 : name:b, age:15, street:peter street
row3 : name:c, age:17, street:james street
Now I would like the query to return
row2 and either row1 or row2
Doesn't matter if it is row1 or row2, i don't care.
Now, ideally i'd like to perform this using hibernate criteria queries because I already have a query builder for that and I am just adding this as an option.
You could try Projections: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html#querycriteria-projection
Something like this could work. It will select unique results based on "street" property:
List results = session.createCriteria(Entity.class)
.setProjection( Projections.projectionList()
.add(Projections.distinct(Projections.property("street")))
)
.list();
这篇关于SQL - 根据不同的属性获取任何行,不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!