简体   繁体   中英

How to match part of rows in different tables with each other in MySQL?

I have a table called 'urls' which contains domain name of different countries like below:

Domain
Paypal.com
wellsfargo.co.uk
sparkasse.fe.de

etc...

and I have another table called 'country' which contains tlds and their associated country like below:

tld        country
nl          netherlands
de          germany
uk          united kingdom
etc...

I do not know how can I create a match in which I would be able to extract the name of the country in the table 'country' from domains in table 'urls'.Can anyone help me with this?

Join the two tables with the following JOIN condition:

ON ( country.tld = RIGHT(urls.domain, CHAR_LENGTH(tld)) )

Notice this query couldn't use an index on urls.domain , if such an index exists. I would precompute the top-level domain of urls.domain on insertion in a separate column (in urls ). Set an index on this new column, and use this column in your JOIN condition.

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