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 8304569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:53:03+00:00 2026-06-08T17:53:03+00:00

Here is my table structure: users: id | email emails: id | subject |

  • 0

Here is my table structure:

users:

id | email

emails:

id | subject | body

user_emails:

user_id | email_id

It’s very simple design. However, I need to select the first (lowest) emails.id for all users.id’s that are not associated in user_emails.

To illustrate:

users:

id | email
1 | email@domain.com
2 | test@lol.com
3 | user@test.com

emails:

id | subject | body
1 | sub1 | body1
2 | sub2 | body2

user_emails:

user_id | email_id
1 | 1
1 | 2
2 | 1

As the data shows:

  • User 1 received email 1 and 2 already so the select should exclude
    that user.
  • User 2 only received email 1, so it should select user 2
    and email 2.
  • User 3 didn’t receive any emails, so it should select user 3
    and email 1.

I’ll be doing this in PHP so any logic should be in written in PHP if it can’t be done with mySQL alone.

Thanks in advance

EDIT: I should probably mention that there will be thousands of user ID’s and hundreds of email id’s. The script that will send out all these emails will run once daily. So, this should be as optimized as possible.

  • 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-06-08T17:53:05+00:00Added an answer on June 8, 2026 at 5:53 pm

    I thought of a much simpler solution to all this. I added a lastSent column to the users table.

    // Load all emails into an array. Use the id as the key.
    $sql = "SELECT * FROM emails";
    $query = mysql_query( $sql );
    while( $row = mysql_fetch_assoc( $query ) ) {
        $emails[ $row['id'] ] = $row;
    }
    
    // Load all users into an array.
    $sql = "SELECT id, email, lastSent FROM users";
    $query = mysql_query( $sql );
    while( $row = mysql_fetch_assoc( $query ) ) {
        $users = $row;
    }
    
    // Loop through each user and send next email from the list based on lastSent value.
    foreach ( $users as $user ) {
        $id = $user['lastSent'] + 1;
        $email = $user['email'];
        $subject = $emails[$id]['subject'];
        $body = $emails[$id]['body'];
        $send = mail($email, $subject, $body);
        if ( $send ) {
                // Update lastSent value.
            $sql = "UPDATE users SET lastSent='$id' WHERE id='" . $user['id'] . "'";
            mysql_query( $sql );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple table structure here. Just a list of words related
Here's my table structure: users --userid --firstname --lastname users_task --userid --taskid I'd like to
Here's the table structure table : user id name email category table 2 :
Here is a table structure (e.g. test): Field Name Data Type id BIGINT (20)
Here is the simplified table structure of PRICES : SKU PriceType FromDate ToDate Price
I have a KVP Table and the structure is ID, Key, Value. Here are
i m having following type of simple sql server table Here I want to
Here an example my database structure.. uid email name ------------------------------- 1 john@gmail.com John 2
I want to do a contacts page. Here is my table structure: Accounts Table:
I have table structure like below: events (boxing, sparring etc) competitors (users who are

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.