那么让我们用这种机制进行 SQL 查询:
假设我想在数据库中找到一个人。人有名字和姓氏,而不仅仅是“姓名”。更复杂的是,Firstname 和 Lastname 都可能包含许多单词。
...所以我们有来自搜索引擎的查询字符串,其中包含 "Jo Svenda Schmidt" ,其中 "Jo Svenda" 是名字。
query = "Jo Svenda Schmidt".split
=> ["Jo", "Svenda", "Schmidt"]
query.permutation(query.length).collect { |x| x.join(" AND ") }.join(" OR ")
=> "Jo AND Svenda AND Schmidt OR Jo AND Schmidt AND Svenda OR Svenda AND Jo AND Schmidt OR Svenda AND Schmidt AND Jo OR Schmidt AND Jo AND Svenda OR Schmidt AND Svenda AND Jo"
This is almost what we want, but not all. We need to add column names and group it.
grouping may be simple, just
... join(" ) OR ( ")
at the and, and then add "(" to the beginning and ")" to the end of resulting query.
But how to add nicely 'Firstname=' and 'Lastname=' or 'LIKE' to this query?