SQL - LIKE在地址字段中搜索

时间:2017-02-06 13:23:58

标签: sql postgresql search full-text-search

我有SQL查询,它在表中搜索地址字段。我的问题出在城市领域。 我假设搜索列街道上有门牌号而且没有他,但我忘记了邻居的划分,如:Kolin IV,KolínII等等。我的问题是,可以某种方式从使用SQL函数的列中仅比较没有城市的名称该地区属于哪种迹象?

$where = " WHERE (LOWER(city) LIKE LOWER('%$fullAddress%')
 OR LOWER(street) LIKE LOWER('%$fullAddress%')
 OR CAST(postal_code AS TEXT) LIKE LOWER('%$fullAddress%')
 OR CAST(house_number AS TEXT) LIKE LOWER('%$fullAddress%')
 OR (LOWER(street || ', ' || city) LIKE LOWER('%$fullAddress%'))
 OR (LOWER(street || ' ' || CAST(house_number AS TEXT)) LIKE LOWER('%$fullAddress%'))
 OR (LOWER(city || ' ' || CAST(postal_code AS TEXT)) LIKE LOWER('%$fullAddress%'))
 OR (LOWER(street || ', ' || city || ' ' || CAST(postal_code AS TEXT)) LIKE LOWER('%$fullAddress%'))
 OR (LOWER(street || ' ' || CAST(house_number AS TEXT) || ', ' || city) LIKE LOWER('%$fullAddress%'))
 OR (LOWER(street || ' ' || CAST(house_number AS TEXT) || ', ' || city || ' ' || CAST(postal_code AS TEXT)) LIKE LOWER('%$fullAddress%'))) AND deleteby is null";
$results = $fce->_slctSQL("public.configurations_view", "", "services", $where, "", " ORDER BY city ASC, street ASC, house_number ASC, postal_code ASC", "");

1 个答案:

答案 0 :(得分:0)

您可能会发现这有效:

 OR (LOWER(city || ' ' || CAST(postal_code AS TEXT)) LIKE LOWER(REPLACE('%$fullAddress%', ' ', '%'))

但是,如果城市有多个单词的名称,这可能会引入其他问题。