我有两张桌子。
表格->帖子(id,title,user_id,…,created_at,…)

table->Reposts(id,post_id,user_id,…created_at,…)
所以,在我的rails应用程序控制器中:

@posts = Post.find(:all, :conditions => ["user_id = ? OR id IN ( select post_id from reposts where user_id=? )", '1', '1'], :limit => 9)

它工作得很好,但是:
我需要通过我的@posts命令,由两列“created_at”组合而成(created_atof table posts和created_atof table REPOSTS)
有什么帮助吗?

最佳答案

试试这个:

class Post
  has_many :reposts
end


class Repost
  belongs_to :post
end

Post.all(
  :include => :reposts,
  :conditions => ["posts.user_id = ? OR reposts.user_id = ?", 1, 1],
  :order => "posts.created_at DESC, reposts.created_at DESC
)

关于mysql - 复杂的SQL语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4454431/

10-12 01:06