我正在尝试在列地址(用户表)中找到在地址(address_effect表)中具有匹配项的任何项目。我正在使用XAMPP在本地系统上对此进行测试(使用MariaDB)
+------------------+-----------------+------------------+--------------------------+ | 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 | | | | | | +------------------+-----------------+------------------+--------------------------+
+---------+----------------+ |idaho |potato, tater | +--------------------------+ |canada |cold, tundra | +--------------------------+ |fremont | crowded | +--------------------------+ |peru |alpaca | +--------------------------+ |atlanta |peach, cnn | +--------------------------+ |usa |big, hard | +--------+-----------------+
我尝试将内部联接与LIKE配合使用以查找匹配的字符串。
如果我使用此查询,则找不到任何项目:
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 || '%')
然后,我尝试了以下查询,它列出了用户表中的所有项目,而不是仅列出那些在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 || '%')
我在这里想念什么?
谢谢。
据我了解的示例数据,您希望将users地址的一部分与另一张表中的值进行匹配。
users
您可能想尝试一下find_in_set()。LIKE匹配更为准确,因为它仅匹配单个元素:
find_in_set()
LIKE
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, ', ', ','))
如果address_effect(address)可以在中存储的CSV列表中找到,则与此匹配users(address)。
address_effect(address)
users(address)