简体   繁体   English

mysql 使用“.” 作为 match(ip_list) against(string) 中的值分隔符,“,” 'string' 也存在

[英]mysql uses "." as a value separator in match(ip_list) against(string), with "," 'string' also present

When processing ip addresses, and comparing them, something goes wrong, and it finds almost all the data from the database.在处理ip个地址,比较时,出错了,从数据库中查到几乎所有的数据。 Query:询问:

SELECT * FROM superbans WHERE NOT uuid=? AND (MATCH(ip_list) AGAINST(?) OR MATCH(clientId_list) AGAINST(?) OR MATCH(deviceId_list) AGAINST(?) OR MATCH(xuid_list) AGAINST(?))

Example uuid: e778e3d4-7866-3601-b069-d38dd1ed2e21示例 uuid: e778e3d4-7866-3601-b069-d38dd1ed2e21

Example ip: 111.111.111.111示例 ip: 111.111.111.111

Example clientId: 4848256256931583384 (but clientId maybe contains "-": -4848256256931583384)示例 clientId: 4848256256931583384 (but clientId maybe contains "-": -4848256256931583384)

Example deviceId: 4a474f8b-3032-35fb-bd27-fb4e3c5435fd示例 deviceId: 4a474f8b-3032-35fb-bd27-fb4e3c5435fd

Example xuid: 2535454407347849示例 xuid: 2535454407347849

If I remove MATCH(ip_list) AGAINST(?) from the query, it outputs the data correctly.如果我从查询中删除 MATCH(ip_list) AGAINST(?) ,它会正确输出数据。 Please help me to make mysql not think that "."请帮助我使 mysql 不认为是“.” this is the delimiter.这是分隔符。

IMPORTANT: in the line with "."重要提示:与“.”一致also has ","也有 ”,”

I tried to do it through REGEXP, because I'm just starting to learn mysql, then I didn't succeed:(我试图通过REGEXP来做,因为我刚开始学习mysql,然后我没有成功:(

Yes, the.是的。 is handled as a delimiter (as are commas, spaces, and many other characters).作为定界符处理(逗号、空格和许多其他字符也是如此)。 To match the exact IP address, put double quotes around each value.要匹配准确的 IP 地址,请在每个值两边加上双引号。 Otherwise, you are going to get results matching anything with 111 (in your example IP).否则,您将获得与 111 匹配的任何结果(在您的示例 IP 中)。

Here is an example to demonstrate the results with and without the double quotes:这是一个示例,用于演示带和不带双引号的结果:

select * from country where countryname='United States';
+-------------+---------------+
| countrycode | countryname   |
+-------------+---------------+
| USA         | United States |
+-------------+---------------+

SELECT *  FROM country  WHERE  MATCH(countryname) AGAINST('United States');
+-------------+--------------------------------------+
| countrycode | countryname                          |
+-------------+--------------------------------------+
| UMI         | United States Minor Outlying Islands |
| USA         | United States                        |
| FSM         | Micronesia, Federated States of      |
| ARE         | United Arab Emirates                 |
| GBR         | United Kingdom                       |
+-------------+--------------------------------------+

SELECT *  FROM country  WHERE  MATCH(countryname) AGAINST('United.States');
+-------------+--------------------------------------+
| countrycode | countryname                          |
+-------------+--------------------------------------+
| UMI         | United States Minor Outlying Islands |
| USA         | United States                        |
| FSM         | Micronesia, Federated States of      |
| ARE         | United Arab Emirates                 |
| GBR         | United Kingdom                       |
+-------------+--------------------------------------+

SELECT *  FROM country  WHERE  MATCH(countryname) AGAINST('"United States"');
+-------------+--------------------------------------+
| countrycode | countryname                          |
+-------------+--------------------------------------+
| UMI         | United States Minor Outlying Islands |
| USA         | United States                        |
+-------------+--------------------------------------+

SELECT *  FROM country  WHERE  MATCH(countryname) AGAINST('"United States","United Kingdom"');
+-------------+--------------------------------------+
| countrycode | countryname                          |
+-------------+--------------------------------------+
| GBR         | United Kingdom                       |
| UMI         | United States Minor Outlying Islands |
| USA         | United States                        |
+-------------+--------------------------------------+

But note you're not leveraging the power of fulltext search if you do this.但请注意,如果您这样做,您并没有利用全文搜索的强大功能。 Do you want to match partial IP addresses?是否要匹配部分 IP 地址? If not, you could just use an IN statement like this example:如果没有,您可以像这个例子一样使用 IN 语句:

select * from country where countryname in ('United States', 'United Kingdom');
+-------------+----------------+
| countrycode | countryname    |
+-------------+----------------+
| GBR         | United Kingdom |
| USA         | United States  |
+-------------+----------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM