Using MySQL, I have the following table called subscriptions
shown below
name, renewal_date, national_id
---------------------------------------
ted, 1/2/2010, A1111R
ben, 1/3/2010, A145E1
bob, 8/4/2009, A11C11
kate, 2/2/2010, A111E1
ted, 12/2/2011, A1111R
bob, 12/2/2014, A11C11
ben, 12/2/2016, A145E1
etc..
The table has about 150000 records. Each person can have multiple subscriptions. I want to display the maximum/latest renewal_date
for each record in a list given to me for querying. Here is my sql statement:
select d.name, d.renewal_date, d.national_id
from subscriptions d
where renewal_date= (select max(d1.renewal_date)
from subscriptions d1
where d1.national_id = d.national_id IN ('A1111R','A11C11', 'A145E1' ....));
When I run the query in phpmyadmin, it does not seem to end executing, even when the records in the IN
clause are few.
Which is the best way to do it? I should also say am not an SQL expert yet :-) Thanks in advance