Hi, I have what I hope will be a quick and easy question.

I am trying to select all records from a table called prod_enroll that were last updated on yesterday's date

This query works, but I was just wondering if there wouldn't be a more elegant way to accomplish this?

The last_update is a datetime field, but the time is irrelevant for this query.

$sql = "
  SELECT mem_id
  FROM prod_enroll
  WHERE date(last_update) > DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND date(last_update) < CURDATE()
  AND rel_code = 'E'
";

Any suggestions would be greatly appreciated.

Thanks
Douglas

Recommended Answers

All 2 Replies

Why not use:

WHERE DATE(last_update) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)

Since you are only checking dates (and not times), no need for the between.

commented: The simpler the better +2

Thank You Sir...

I would have sworn that I had tried that variation yesterday, but apparently on that I missed.

Was getting lost in trying to match the date only in a datetime field, and apparently overthinking it. Not unusual for me.

That worked just fine...

Thanks for your help.
Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.