0

想象一下以下联系人:

{
  first_name:"Walter",
  last_name:"White"
}

我希望以下搜索字符串返回该联系人:

  • Walter White
  • White Walter

但是,上述搜索字符串不应返回以下联系人:

{
  first_name:"Skylar",
  last_name:"White"
}

{
  first_name:"Walter",
  last_name:"Grey"
}

我试图通过编写一个 sqlite 谓词来实现这一点。

我在想这一定是一个非常普遍的问题,但我还没有在这里找到可以回答这个问题的问题。

4

3 回答 3

1
SELECT *
FROM MyTable
WHERE first_name || ' ' || last_name = ?
   OR last_name || ' ' || first_name = ?
于 2012-12-06T12:29:22.690 回答
0

Working from the comments in CL's answer: You're talking about doing partial matches. e.g. Mich returns Michael, Walt returns Walter, Mary returns Mary Sue, etc.

EDIT:

Using a little reverse LIKE trickery and some input padding, you get this:

SELECT *
FROM People_Table
WHERE first_name || ' ' last_name LIKE '%' || (SELECT replace(inputString, ' ', '%')) || '%'
   OR last_name || ' ' first_name LIKE '%' || (SELECT replace(inputString, ' ', '%')) || '%'

For double names like "Mary Sue Jones", the following will all match: "Mary Jones", "Jones", "Sue Jones", "Jones Sue", "Sue Mary", "Mary Sue Jones", etc. The drawback to this method is that if the portions of a double name are out of order in the search string, it will not match. You'll have to go outside this SQLite predicate to deal with that.

于 2013-03-08T19:53:37.757 回答
0

首先分离 & 然后搜索 FirstName = firstname 和 LastName = lastname 然后反转以防万一用户首先输入姓氏

try {       
    int firstSpace = search_str.indexOf(" "); // detect the first space character
    firstName = search_str.substring(0, firstSpace);  // get everything upto the first space character
    lastName = search_str.substring(firstSpace).trim(); // get everything after the first space, trimming the spaces off
        }
catch (Exception c){}

FIRST_NAME LIKE '%"+firstName+"%' AND LAST_NAME LIKE '%"+lastName+"%' OR FIRST_NAME LIKE '%"+lastName+"%' AND LAST_NAME LIKE '%"+firstName+"%'

于 2015-05-04T14:40:34.403 回答