简体   繁体   English

如何在另一个字段的任何行中查找具有一个字段内容的MySQL记录

[英]How to find MySQL records having one field's content within any row of another field

The problem is simple. 问题很简单。 Is there a MySQL query allowing, in a single table, to find all records having some content within any row of another column ? 是否存在一个MySQL查询,该查询允许在单个表中查找另一列的任何行中具有某些内容的所有记录?

In other words, I have a table with 2 columns : "FirstName" and "LastName". 换句话说,我有一个包含2列的表:“名字”和“姓氏”。

My automatic filler has melted some of them. 我的自动填充器融化了其中一些。

So I would like to find all occurences of "LastName"s that also appear in column "FirstName". 因此,我想找到所有出现在“ FirstName”列中的“ LastName”。

That way, all the Doe JOHN would be detected, because "John" must 100% be in the list of "FirstName"s, whereas John DOE would not be detected, because there is nearly no chance to find DOE as a first name... 这样,将检测到所有Doe JOHN,因为“ John”必须100%位于“ FirstName”列表中,而不会检测到John DOE,因为几乎没有机会将DOE作为名字。 ..

Just perform a self join over the table. 只需在桌子上进行自我联接即可。 It should look something like this: 它看起来应该像这样:

SELECT 
    *
FROM
    CUSTOMER C1
        INNER JOIN
    CUSTOMER C2 ON (C1.FirstName = C2.LastName OR C1.LastName = C2.FirstName);

I'm guessing you want something like: 我猜你想要这样的东西:

SELECT * 
FROM nametable
WHERE firstname IN (select lastname FROM nametable)

That will find row where the first name exists as a last name in the table. 这将在表中找到名字作为姓氏存在的行。

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

相关问题 如何在mysql中将整个行从一个表复制到另一个表,而第二个表具有修改后的字段 - How to copy an entire row from one table to another in mysql with the second table having a modified field 如何根据另一个字段查找特定时间范围内的记录 - How to find records within a certain timeframe based on another field 如何根据MySQL中另一个表的id字段查找表中的记录? - How to find records in table based on another table's id field in MySQL? 根据另一个字段查找MySQL行的ID - Find the ID of a MySQL row based on another field MySQL查询查找一个字段出现到另一个字段 - MySQL query to find occurence of one field into another 我怎么知道一个表的主键字段值在MySQL的任何其他相关表中使用? - How do I know one table's primary key field value is used in any another related tables in MySQL? 如何从字段包含mysql中的字段(1,2,3)之类的字段ID的字段中获取记录? - how to get records from field having contain field ids like (1,2,3) in a field in mysql? MySQL的。 将一个表中的记录与另一个表中不具有公共字段的合并 - MySQL . Combine records from one table with another table with not common field mySQL 查询以在单个字段中以任意顺序查找多个字符串? - mySQL query to find multiple strings in any order within a single field? 如何使用PHP获取MySQL字段的内容? - How to get a MySQL field's content with PHP?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM