Possible duplicate of: How to select date from datetime column?
But the problem with the accepted answer is it will preform a full table scan.
I want to do something like this:
UPDATE records SET earnings=(SELECT SUM(rate)
FROM leads
WHERE records.user_id=leads.user_id
AND DATE(leads.datetime)=records.date)
Notice the last portion: DATE(leads.datetime)=records.date
. This does exactly what it needs to do, but it has to scan every row. Some users have thousands of leads so it can take a while.
The leads
table has an INDEX
on user_id
,datetime
.
I know you can use interval functions and do something like WHERE datetime BETWEEN date AND interval + days
or something like that.
What is the most efficient and accurate way to do this?