Will R.
Will R.

Reputation: 120

Compare last 10 characters in mysql?

I have two tables: contacts and messages.

contacts has: person, address

messages has: address, message

address can have more than 10 characters.

The address in contacts might have 13 characters: abc9995551212

The address in messages might have 11 characters: e9995551212

How can I compare the last 10 characters from both addresses

and if they match create a view with:

contacts.person, messages.address, messages.message

Upvotes: 1

Views: 786

Answers (1)

Mureinik
Mureinik

Reputation: 311528

The RIGHT function can be used to return the n right (last) characters of a string. From there on, it's just a matter of joining:

SELECT contacts.person, messages.address, messages.message
FROM   contacts
JOIN   messages ON RIGHT(messages.address, 10) = RIGHT(contacts.address, 10)

Upvotes: 7

Related Questions