【发布时间】:2020-06-25 16:16:43
【问题描述】:
我正在尝试查找列地址(用户表)中与地址(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 |
| | | | |
+------------------+-----------------+------------------+--------------------------+
address_effect表
+---------+----------------+
|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 || '%')
我在这里错过了什么?
谢谢。
【问题讨论】:
-
在您的示例数据中,所有内容都有匹配项。
-
你是对的!我错误地在 address_effect 表中添加了不应该存在的最后几条记录。 @GMB 使用 find_in_set 查询似乎有效
标签: sql csv join mariadb inner-join