我正在尝试在列地址(用户表)中找到在地址(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 || '%')
我在这里想念什么?
谢谢。
答案 0 :(得分:0)
据我所知,您想将users
地址的一部分与另一张表中的值进行匹配。
您可能想尝试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)
,则此匹配。