简体   繁体   中英

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 ?

In other words, I have a table with 2 columns : "FirstName" and "LastName".

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".

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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