1

I have a MySQL table of auto-generated tags, and I'm wanting to delete any shorter tags that appear within longer tags.

For example, say SELECT * FROM tags returns

Donald Trump
Donald
Trump
Hillary Clinton
Hillary
Clinton
US Election
US
Election

I'm wanting to delete any shorter tags that appear within longer tags, so I end up with:

Donald Trump
Hillary Clinton
US Election

Is this possible?

Thank you.

user2597933
  • 63
  • 10

1 Answers1

2

Join the table with itself, and delete the rows with shorter names that can be found inside the longer name.

DELETE t1
FROM tags AS t1
JOIN tags AS t2 ON LENGTH(t1.name) < LENGTH(t2.name) AND LOCATE(t1.name, t2.name)
juergen d
  • 201,996
  • 37
  • 293
  • 362
Barmar
  • 741,623
  • 53
  • 500
  • 612