简体   繁体   中英

how can i write a sql query that finds rows where one column is a substring of another column

I wish to find all rows in a table where one column is a substring of another column.

In other words, suppose I have a table (called people) with two columns: firstname and lastname, and I want to find all people like "rob robinowitz" and "jill bajillion".

Is there a way to do something like "select * from people where lastname like %firstname%"? (But something which actually works).

You were close

select * from people where lastname like '%' + firstname + '%'

Alternative way (may be even faster)

select * from people where charindex(firstname,lastname)>0

如果您使用的是MySQL,则可以

SELECT * FROM people WHERE INSTR(lastname, firstname) <> 0

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