简体   繁体   English

MYSQL / PHP如何查找以相同单词开头的所有行?

[英]MYSQL/PHP How to find all rows that start with the same word?

in my DB i have a table with brand names, and I need to clean it up since i have some duplicates like 在我的数据库中,我有一个带有品牌名称的表,我需要清理它,因为我有一些重复项,例如

Yadda 亚达

Yadda & Co 亚达公司

Yadda Engineering 雅达工程

which are the same brand but have been entered by mistake multiple times despite the column is unique 相同的品牌,但尽管该列是唯一的,但仍多次输入错误

how to approach this? 如何处理呢? can this be done in mysql query only? 只能在mysql查询中完成吗?

thanks 谢谢

My offer first create FullText index for search column. 我的报价首先为搜索列创建全文索引。 After with full text score you can determine similarity of strings. 在获得全文评分之后,您可以确定字符串的相似性。 Delete them unless which have greater score and greate than threshold. 除非分数和阈值大于阈值,否则将其删除。

DELETE FROM table_name 
WHERE
    id <> (SELECT id FROM table_name ORDER BY MATCH (title) AGAINST ('Yada' IN NATURAL LANGUAGE MODE) DESC LIMIT 0,1) AND
    MATCH (title) AGAINST ('Yada' IN NATURAL LANGUAGE MODE) < threshold

Also instead off Fulltext you can use levenshtein function in this answer (with same logic sql query) how to compute similarity between two strings in MYSQL 也可以关闭全文,而不是在此答案中使用levenshtein函数(使用相同的逻辑sql查询) 如何在MYSQL中计算两个字符串之间的相似度

You can use substr 您可以使用substr

assuming that your rows begin all with the words 'Yadda' you can 假设您的所有行都以“ Yadda”开头,则可以

 select * from my_table 
 where substr(my_column, 1, length('Yadda')) = 'Yadda');

Then if you need delete the improper rows you can (assuming that you want save the rows with 'Yadda & Co' you could 然后,如果您需要删除不适当的行,则可以(假设您要使用“ Yadda&Co”保存行,则可以

delete from my_table
where substr(substr(my_column, 1, length('Yadda')) = 'Yadda')
and my_column <> 'Yadda & Co';

or for American 或美国人

delete from my_table
where substr(substr(my_column, 1, length('American')) = 'American')
and my_column not in  ('American Standard', 'American Airlines');

SELECT Name FROM Table WHERE Name REGEXP '^[Y].*$'

In this REGEXP stands for regular expression REGEXP在此代表正则表达式

and

this is for T-SQL 这是用于T-SQL

SELECT Name FROM Table WHERE Name LIKE '[Y]%'

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

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