1

我想根据 mysql 表中的两个字段选择一条记录两次。

例如我的表有以下数据:

 name,   created_at,  updated_at
"Baran", "2013-09-10 23:56:56", "2013-10-10 23:00:00"

我想要一个按降序排列的结果集:

name,date
Baran,"2013-10-10 23:00:00"
Baran,"2013-09-10 23:56:56"

如果是的话,是否可能,那么请建议我如何实现这一目标?

4

2 回答 2

1
(SELECT name, created_at as [date] FROM mytable)
  UNION all
(SELECT name, updated_at as [date] FROM mytable)
  ORDER BY name, [date] desc;
于 2013-11-01T10:04:07.483 回答
0

您可以像往常一样选择您的记录并在 Ruby 中创建“重复项”:

User.all.flat_map { |user|
  [
    {name: user.name, date: user.created_at},
    {name: user.name, date: user.updated_at}
  ]
}
#=> [{:name=>"Baran", :date=>"2013-09-10 23:56:56"}, {:name=>"Baran", :date=>"2013-10-10 23:00:00"}]
于 2013-11-01T10:26:49.727 回答