Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4602882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:03:32+00:00 2026-05-22T00:03:32+00:00

I have two tables: one with emails and the other table with domains. Table1

  • 0

I have two tables: one with emails and the other table with domains.

Table1                  Table2
id email                id  domain
-- ----                 --  ----
1  name@domain1.com     1   domain1.com
2  name@domain2.com     2   domain4.com
3  name@domain3.com
4  name@domain4.com

Now I want to select all email from table 1 where the domain matches the domain field of table 2. The result should be:

id email
-- ----
1  name@domain1.com
4  name@domain4.com

I guess it would work with a combination of REGEXP and INNER JOIN? But I don’t know how to combine them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:03:33+00:00Added an answer on May 22, 2026 at 12:03 am

    SOlution 1 :

    Use Table1.email LIKE CONCAT('%@',Table2.domain).

    BIG FAT RED WARNING : This condition is not indexable. If you want speed, split the email into address and domain in Table1, and create an index.

    EDIT :

    In fact this JOIN condition will need count(email)xcount(domain) comparisons, so it’s probably the slowest way.

    Solution 2:

    Rob has a very good point : a faster way is to extract the domain from the email (using substring) and match this against an index on the domains table

    SOlution 3 :

    The best way is to index the email’s domain. In Postgres you would create a function index ON extract_domain(email) (supposing you create a trivial extract_domain() function), but you cannot do this in MySQL, so an extra column with the domain only is the way to go for speed.

    If you want to know all emails in ONE domain,

    Solution 1 : seq scan table emails + fast LIKE
    Solution 2 : seq scan table emails + slightly slower domain extraction
    Solution 3 : index scan table emails
    

    If you want to JOIN on table domains for all domains / all emails :

    Solution 1 : count(email)xcount(domain) comparisons, very slow
    Solution 2 : seq scan table emails + index scan domains
    Solution 3 : nested loop index join
    

    For a full JOIN it would be even faster to use a merge join or hash join, but those are not provided by mysql.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables (customers, admin) A user logs in by email and password,
I have two tables one with jobs one with managers, when a job ID
I have two tables, jos_eimcart_customers_addresses and jos_eimcart_customers. I want to pull all records from
I am writing an email application that interfaces with a MySQL database. I have
I have an app that supports email subscriptions, surveys, and comment forms. I've got
I am trying to create a report using BIDS. I want one of the
I have a working web application which already has a login and registration system.
I'm trying to delete records from one database based on a selection criteria of
I've been banging my head against the wall with a really annoying issue. I
I built a site using Drupal 6 recently that allows for users to submit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.