开发者

Translating rails queries for sqlite

开发者 https://www.devze.com 2023-01-13 19:37 出处:网络
How would i translate these mysql queries to work with sqlite3?: self.find(:first, :conditions => [\'concat(first_name, \\\' \\\', middle_names, \\\' \\\', last_name) = ?\', name])

How would i translate these mysql queries to work with sqlite3?:

self.find(:first, :conditions => ['concat(first_name, \' \', middle_names, \' \', last_name) = ?', name])

self.find(:all, :conditions => ['(concat(first_name, \' \', last_name) L开发者_StackOverflow中文版IKE ?) OR (concat(first_name, \' \', middle_names, \' \', last_name) LIKE ?)', "%#{name}%", "%#{name}%"])


A little google search could have helped: http://www.google.com/search?q=sqlite+concat

In SQLite the concat operator is ||. and for easier reading of string with single quotes ' I would enclose these strings with double quotes ", so you don't need so much escaping:

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}%"])


|| is the SQLite concatenation operator. So this'll probably work:

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}%"])
0

精彩评论

暂无评论...
验证码 换一张
取 消