3

I have a table of around 10 sales reps, and each one should be sent a new email containing a prospect, each time a new prospect is added to the database.

---------------------------------------------
id  |  name  |  email               |  active
---------------------------------------------
1   |  bill  |  bill@billspills.com |    y
2   |  bob   |  bob@bobsjobs.com    |    n
3   |  sue   |  sue@suesshoes.com   |    y
4   |  betty |  jan@jansvans.com    |    y

I would like the system to email the reps in a round robin manner. Once the end of the table is reached, it starts from the beginning again. I only want to email to the ones with the 'active' flag set to 'y'. The reps have the ability to opt out by setting the flag to 'n'. So, the above would email:

bill, sue, jan, bill, sue, jan etc.

I need this to be persistent, as the leads may come in minutes or hours apart, so I need a way to remember the last emailed rep, to determine who should get the next email.

I considered using a fifth column, 'emailedLast', and then querying the person who got emailed last, and selecting the next ID. I'm not sure how I'd get back to the start again, perhaps by counting the records to determine the last one in the table.

I'm asking, just to see if there isn't a much simpler, cleaner method that I've missed. I'm using PHP for this.

Update:

Solution: (using Laravel/Eloquent)

public function touch(){
        $rep = Rep::where('active','=','y')->orderby('updated_at', 'asc')->first();
        $rep->touch();
        echo $rep->name;
}

Equivalent using SQL:

select * from `reps` where `active` = 'y' order by `updated_at` asc limit 1 
update `reps` set `updated_at` = '2015-01-28 03:52:32' where `id` = '2' 
Chris J Allen
  • 18,970
  • 20
  • 76
  • 114

1 Answers1

3

Add an emailedLast column (timestamp) and when you email a lead to a rep, set it to the current timestamp (now())

Then your query to look up the next rep to email would be:

select * from reps where active = 'y' order by date asc limit 1;
mdm
  • 12,480
  • 5
  • 34
  • 53