简体   繁体   English

我无法正确加入REGEXP mysql

[英]I can't get a RIGHT JOIN to work with REGEXP mysql

I'm sick of beating by head over the wall with this one. 我厌倦了与这个人头撞墙。 So far nobody on our team can figure out why it's not working. 到目前为止,我们团队中没有人能弄清楚为什么它不起作用。

I have two tables that I need to check rows against each other on each of them for accuracy. 我有两个表,我需要检查每个表的行之间的准确性。

Table1 表格1

feature_name | state_alpha | population data
city1        |     MI      |          34567
city3        |     MI      |          4567
city4        |     MI      |          5567

Table2 表2

city
city1
city2
city3

I have gotten the LEFT JOIN to work with this query: 我已经获得了LEFT JOIN来处理以下查询:

SELECT f.feature_name, f.population_data, f.state_alpha, bc.city
FROM `fedcodes` AS f
left JOIN bob_cities AS bc ON bc.city
REGEXP concat( f.feature_name )
WHERE f.population_data >1000
AND f.state_alpha = "MI"
AND f.feature_name NOT
REGEXP 'city of'
AND f.feature_name NOT regexp 'town of'

This returns exactly what I expect it to. 这恰好返回了我期望的结果。 All values in the left column are there and NULL values in the right column where there are no matches. 左列中的所有值都存在,而右列中没有匹配项的情况下为NULL值。 Which solves what I need to know: "Which cities are in table1 that are not also in table2." 这就解决了我需要知道的问题:“表1中哪些城市不在表2中。”

Next I need to know which cities are in table2 that are not in table1 based on the WHERE clause. 接下来,我需要根据WHERE子句了解table2中哪些城市不在table1中。 A RIGHT JOIN should do this. 一个正确的联接应该做到这一点。 Correct? 正确?

However I cannot get it to work and I feel like I have tried every possible permutation of the query and have crashed my home server quite a few time in the process. 但是我无法使其正常工作,我感觉我已经尝试了查询的所有可能排列,并使我的家庭服务器在此过程中崩溃了很多时间。

Why doesn't this work? 为什么不起作用?

SELECT f.feature_name, f.population_data, f.state_alpha, bc.city
FROM `fedcodes` AS f
RIGHT JOIN bob_cities AS bc ON f.feature_name
REGEXP concat( bc.city )
WHERE f.population_data >1000
AND f.state_alpha = "MI"
AND f.feature_name NOT
REGEXP 'city of'
AND f.feature_name NOT regexp 'town of'

I've also tried this. 我也试过了。

SELECT f.feature_name, f.population_data, f.state_alpha, bc.city
FROM `fedcodes` AS f
RIGHT JOIN bob_cities AS bc ON (f.feature_name
REGEXP concat( bc.city )
AND f.population_data >1000
AND f.state_alpha = "MI"
AND f.feature_name NOT
REGEXP 'city of'
AND f.feature_name NOT regexp 'town of')

I'm using REGEXP because even though the names in the rows are the same in both tables (feature_name = city) if I use the = operator it doesn't work(even in the LEFT JOIN that I have working) 我使用REGEXP是因为即使使用=运算符,即使两个表中的行名称都相同(feature_name = city),它也不起作用(即使在我已经使用过的LEFT JOIN中)

All that happens when I run the queries in phpmyadmin is the server stays busy for a while then freezes. 当我在phpmyadmin中运行查询时,所有发生的事情是服务器保持繁忙一段时间,然后冻结。

Try nesting fedcodes , with the other WHERE clauses that only it needs: 尝试嵌套fedcodes ,与其他WHERE子句只是需要的是:

SELECT f.feature_name, f.population_data, f.state_alpha, bc.city
FROM (SELECT feature_name, population_data, state_alpha
    FROM `fedcodes`
    WHERE population_data >1000
    AND state_alpha = "MI"
    AND feature_name NOT
    REGEXP 'city of'
    AND feature_name NOT regexp 'town of'
    ) AS f
RIGHT JOIN bob_cities AS bc ON f.feature_name
REGEXP concat( bc.city )

It may be that your query is having trouble performing matches on null values? 可能是您的查询无法对空值执行匹配?

Just flip around the order of the joins ... 只是翻转连接的顺序...

 SELECT f.feature_name, f.population_data, f.state_alpha, bc.city
   FROM bob_cities as bc
   LEFT JOIN `fedcodes` as f ON   ON bc.city  REGEXP concat( f.feature_name )
   ...

That should work. 那应该工作。

If you're crashing mysqld with a query you should consider reporting a bug to the mysql team. 如果使用查询使mysqld崩溃,则应考虑向mysql团队报告错误。

Try fixing the REGEXP portion of the second query to match the first: 尝试修复第二个查询的REGEXP部分以匹配第一个查询:

SELECT f.feature_name, f.population_data, f.state_alpha, bc.city
FROM `fedcodes` AS f
RIGHT JOIN bob_cities AS bc ON bc.city REGEXP f.feature_name
...

Better yet use LOCATE (assuming = won't work, = is even better): 最好还是使用LOCATE (假设=无效, =甚至更好):

SELECT f.feature_name, f.population_data, f.state_alpha, bc.city
FROM `fedcodes` AS f
RIGHT JOIN bob_cities AS bc ON 0 < LOCATE(TRIM(f.feature_name), TRIM(bc.city))
...

Remember, REGEXP usually expects a pattern. 请记住, REGEXP通常期望一个模式。 It's a lot more complicated than contains() or equals(). 它比contains()或equals()复杂得多。

First I would like to thank everyone for their response. 首先,我要感谢大家的回应。 I tried everything that you guys suggested to no avail. 我尝试了你们提出的所有建议,但均无济于事。 But as it turns out it wasn't the query after all. 但事实证明,这毕竟不是查询。 Which is why nothing I tried worked. 这就是为什么我没有尝试的原因。

A team member found that the data in the database was corrupt somehow. 一个团队成员发现数据库中的数据由于某种原因已损坏。 I'm not sure of the details but the origin of the data was from a CSV and he said that it was bad in there. 我不确定细节,但是数据的来源是CSV,他说那里的数据不好。 He gave me a new copy of the CSV (cleaned) I recreated the database with it and everything worked as it should. 他给了我CSV的新副本(已清理),我用它重新创建了数据库,并且一切正常进行。

Here is the working query: 这是工作查询:

SELECT DISTINCT f.feature_name, f.population_data, b.city
FROM `fedcodes` f
RIGHT JOIN bob_cities_fix b ON f.feature_name = b.city
AND f.population_data =0
AND f.state_alpha = "al"

A RIGHT JOIN by the book working exactly the way it needs to. 这本书的正确结合完全按照其需要的方式工作。

You may notice in the original query I was filtering the data of cities over 1000 and it turns out I needed to find cities with no population in the database for the RIGHT JOIN to accurately return what I needed. 您可能会注意到,在原始查询中,我正在过滤1000多个城市的数据,结果我需要在数据库中查找没有人口的城市,以使RIGHT JOIN能够准确返回我需要的数据。

However this was an easy fix once I was getting feedback from the queries rather than freezing PHPmyadmin. 但是,一旦我从查询中获得反馈,而不是冻结PHPmyadmin,这是一个简单的修复。

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

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