代码之家  ›  专栏  ›  技术社区  ›  thomasfedb

为sqlite转换rails查询

  •  0
  • thomasfedb  · 技术社区  · 14 年前

    self.find(:first, :conditions => ['concat(first_name, \' \', middle_names, \' \', last_name) = ?', name])
    
    self.find(:all, :conditions => ['(concat(first_name, \' \', last_name) LIKE ?) OR (concat(first_name, \' \', middle_names, \' \', last_name) LIKE ?)', "%#{name}%", "%#{name}%"])
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   jigfox    14 年前

    http://www.google.com/search?q=sqlite+concat

    在SQLite中,concat操作符是 || . 为了更容易阅读带单引号的字符串 ' 我会用双引号把这些字符串括起来 " ,所以你不需要太多的逃避:

    self.find(:first, :conditions => ["first_name || ' ' || middle_names || ' ' || last_name = ?", name])
    
    self.find(:all, :conditions => ["(first_name || ' ' || last_name LIKE ?) OR (first_name, ' ', middle_names || ' ' || last_name LIKE ?)", "%#{name}%", "%#{name}%"])
    
        2
  •  0
  •   Albert Peschar    14 年前

    self.find(:first, :conditions => ['first_name || \' \' || middle_names || \' \' || last_name = ?', name])
    self.find(:all, :conditions => ['(first_name || \' \' || last_name LIKE ?) OR (first_name || \' \' || middle_names || \' \' || last_name LIKE ?)', "%#{name}%", "%#{name}%"])