SQL query to find partial string match across 2 tables

自古美人都是妖i 提交于 2020-08-20 12:42:15

问题


I'm trying to find any items in column address (users table) that have a match in Address (address_effect table). I'm testing this on my local system with XAMPP (using MariaDB)

user table

+------------------+-----------------+------------------+--------------------------+
|        ID        |    firstname    |  lastname        |    address               |
|                  |                 |                  |                          |
+----------------------------------------------------------------------------------+
|     1            |    john         |    doe           |james street, idaho, usa  |                    
|                  |                 |                  |                          |
+----------------------------------------------------------------------------------+
|     2            |    cindy        |   smith          |rollingwood av,lyn, canada|
|                  |                 |                  |                          |
+----------------------------------------------------------------------------------+
|     3            |    rita         |   chatsworth     |arajo ct, fremont, cali   |
|                  |                 |                  |                          |
+------------------+-----------------+---------------------+-----------------------+
|     4            |    randy        |   plies          |smith spring, lima, peru  |                       
|                  |                 |                  |                          |
+----------------------------------------------------------------------------------+
|     5            |    Matt         |   gwalio         |park lane, atlanta, usa   |
|                  |                 |                  |                          |
+------------------+-----------------+------------------+--------------------------+

address_effect table

+---------+----------------+
|idaho    |potato, tater   |
+--------------------------+
|canada   |cold, tundra    |
+--------------------------+
|fremont  | crowded        |
+--------------------------+
|peru     |alpaca          |
+--------------------------+
|atlanta  |peach, cnn      |
+--------------------------+
|usa      |big, hard       |
+--------+-----------------+

I've tried using inner join with LIKE to find matching string.

If I use this query it doesn't find any items:

SELECT users.firstname, users.lastname, users.address
FROM users
INNER JOIN db_name.address_effect
ON
(address_effect.Address LIKE '%' + users.address + '%'
OR users.address LIKE '%' || address_effect.Address || '%')

Then I tried the following query, it lists all the items from user table, instead of only those items that have a match in address_effect

SELECT DISTINCT users.firstname, users.lastname, users.address
FROM users
INNER JOIN db_name.address_effect
ON
(address_effect.Address LIKE '%' || users.address || '%'
OR users.address LIKE '%' || address_effect.Address || '%')

What am I missing here?

Thanks.


回答1:


As I understand your sample data, you want to match parts of users addresses against values in the other table.

You might want to try find_in_set(). It is more accurate that a LIKE match, since it matches on individual elements only:

SELECT u.firstname, u.lastname, u.address user_address, a.*
FROM users u
INNER JOIN address_effect a 
    ON FIND_IN_SET(a.address, REPLACE(u.address, ', ', ','))

This matches if address_effect(address) can be found in the CSV list stored in users(address).



来源:https://stackoverflow.com/questions/62579803/sql-query-to-find-partial-string-match-across-2-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!