In Rails 3, how do i select rows based on unique column values, i need to get all the columns for eg:
在Rails 3中,如何根据唯一列值选择行,我需要获取所有列,例如:
SELECT COUNT(DISTINCT date) FROM records
This only returns date column, but i want all the columns (name, date , age , created_at) columns not just the date.
这只返回日期列,但我希望所有列(名称,日期,年龄,created_at)列不仅仅是日期。
Thanks for your help
谢谢你的帮助
4 个解决方案
#1
7
The issue here is that, by definition, there may be multiple records with the same date. It requires logic in the user space to determine which of the multiple records with the unique date to use. Here's some code to get those rows:
这里的问题是,根据定义,可能存在具有相同日期的多个记录。它需要用户空间中的逻辑来确定具有唯一日期的多个记录中的哪一个使用。以下是获取这些行的一些代码:
Record.select("distinct date").each do |record|
records = Record.find_by_date record.date
puts records.count # do something with the records
end
If what you're really after is uniqueness among multiple columns, list all the relevant columns in the distinct query:
如果您真正追求的是多列之间的唯一性,请列出不同查询中的所有相关列:
Record.select("distinct date, name, age, created_at").each do |record|
puts record.date
puts record.name
puts record.age
puts record.created_at
# ``record'' still represents multiple possible records
end
The fact that you are using distinct means that each "row" returned actually represents n rows, so the DB doesn't know which of the n rows to pull the remaining columns from. That's why it only returns the columns used in distinct. It can do no other...
您使用distinct的事实意味着返回的每个“行”实际上代表n行,因此DB不知道从哪个n行拉出剩余的列。这就是为什么它只返回distinct中使用的列。它没有别的......
#2
5
I think this will help you
我想这会对你有所帮助
Model.find(:all, :select => 'DISTINCT name, date, age, created_at')
Please use it and let me know.
请使用它,让我知道。
#3
4
Model.group(:column)
For your case:
对于你的情况:
Record.group(:date)
This will return all your columns with no "date" repetitions.
这将返回所有列,没有“日期”重复。
#4
1
For rails 3.2 and higher, Model.select('DISTINCT name, date, age, created_at')
对于rails 3.2及更高版本,Model.select('DISTINCT name,date,age,created_at')
#1
7
The issue here is that, by definition, there may be multiple records with the same date. It requires logic in the user space to determine which of the multiple records with the unique date to use. Here's some code to get those rows:
这里的问题是,根据定义,可能存在具有相同日期的多个记录。它需要用户空间中的逻辑来确定具有唯一日期的多个记录中的哪一个使用。以下是获取这些行的一些代码:
Record.select("distinct date").each do |record|
records = Record.find_by_date record.date
puts records.count # do something with the records
end
If what you're really after is uniqueness among multiple columns, list all the relevant columns in the distinct query:
如果您真正追求的是多列之间的唯一性,请列出不同查询中的所有相关列:
Record.select("distinct date, name, age, created_at").each do |record|
puts record.date
puts record.name
puts record.age
puts record.created_at
# ``record'' still represents multiple possible records
end
The fact that you are using distinct means that each "row" returned actually represents n rows, so the DB doesn't know which of the n rows to pull the remaining columns from. That's why it only returns the columns used in distinct. It can do no other...
您使用distinct的事实意味着返回的每个“行”实际上代表n行,因此DB不知道从哪个n行拉出剩余的列。这就是为什么它只返回distinct中使用的列。它没有别的......
#2
5
I think this will help you
我想这会对你有所帮助
Model.find(:all, :select => 'DISTINCT name, date, age, created_at')
Please use it and let me know.
请使用它,让我知道。
#3
4
Model.group(:column)
For your case:
对于你的情况:
Record.group(:date)
This will return all your columns with no "date" repetitions.
这将返回所有列,没有“日期”重复。
#4
1
For rails 3.2 and higher, Model.select('DISTINCT name, date, age, created_at')
对于rails 3.2及更高版本,Model.select('DISTINCT name,date,age,created_at')