sql查询以查找可能包含特殊字符的部分字符串匹配

qnzebej0  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(358)

带特殊字符的sql查询()
最初的查询(感谢gmb)可以在address(users表)中找到与address(address\u effect表)匹配的任何项。
如果address包含','查询可以正常工作,但是如果address字段中有'()'我似乎无法使其工作。
以下是不起作用的sql查询:

UPDATE users u 
SET u.COUNT = (
  SELECT COUNT(*) FROM address_effect a 
  WHERE FIND_IN_SET(a.address, REPLACE(u.address, ', ', ','')')) 
)

仅供参考,我正在用xampp(使用mariadb)在本地系统上测试这个。
我试图将“()”标识为转义字符,方法是在其前面加上反斜杠“”,但没有帮助。

用户表

+--------+-------------+---------------+--------------------------+--------+
|    ID  |  firstname  |  lastname     |    address               |  count |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     1  |    john     |    doe        |james street, idaho, usa  |        |                    
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     2  |    cindy    |   smith       |rollingwood av,lyn, canada|        |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     3  |    rita     |   chatsworth  |arajo ct, alameda, 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       |
+--------+-----------------+
t30tvxxf

t30tvxxf1#

我建议对此使用正则表达式。这似乎比摆弄琴弦更一般:

update users u
    set count = (select count(*)
                 from address_effect ae
                 where u.address regexp concat('[[:<:]]', ae.address, '[[:>:]]'))
                );

funky character类是mysql描述单词边界的方式(我更习惯于 \W 但mysql不支持这一点)。
这是一把小提琴。

wgx48brx

wgx48brx2#

就像用逗号替换每个逗号后面的空格一样,使用 REPLACE() 去焦 '(' 以及 ')' :

FIND_IN_SET(a.address, REPLACE(REPLACE(REPLACE(u.address, ', ', ','), '(', ''), ')', ''))

请看演示。
结果:

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

相关问题