I have this table pageview
id | visitor | id_realestate | time
-----------------------------------------------
I want the record to be inserted if only the last time is 30 mins old at least for that particular id_realestate which has been checked by that particular visitor
update
to make it clearer. If I have this data ready to be inserted in the table
visitor:"axax"
id_realestate:120
time:NOW()
these value should be inserted if only the last record with these values visitor:"axax" & id_realestate:120 is older than 30 minutes (by checking the field time for the last record of the two values I mentioned and compare it with NOW() if the difference is greater than 30 minutes. if so then the data should be inserted)
update
I have this these data now. as you can see the time field contains a value from yesterday so any new data should be inserted:

I used this query as suggested by anubhava but nothing happened ( no new record added/ no errors)
$query='insert into pageview
select "q17872745t", 150, now()
from pageview a
where id_realestate=150 and DATE_ADD(now(),INTERVAL -30 MINUTE) > (
select max(time) from pageview b where a.id_realestate=b.id_realestate AND a.visitor=b.visitor
)';
update
I modified the query to the following and it’s working BUT It compares with every time if the latest time is 30 mint old and accordingly it inserts a record( meaning it doubles the records). so if I have two records with same visitor and id_realestate it will compare with these two records if the time in both is 30 minutes old then it inserts 2 identical records whereas it should insert one record.
$query='insert into pageview (visitor,id_realestate,time)
select "q17872745t", 150, now()
from pageview a
where id_realestate=150 and DATE_ADD(now(),INTERVAL -30 MINUTE) > (
select max(time) from pageview b where a.id_realestate=b.id_realestate AND a.visitor=b.visitor
)';
update: [solved]
I added LIMIT 1 at the end of the above query, now it’s working as it should. here is the final query:
$query='insert into pageview (visitor,id_realestate,time)
select "q17872745t", 150, now()
from pageview a
where id_realestate=150 and DATE_ADD(now(),INTERVAL -30 MINUTE) > (
select max(time) from pageview b where a.id_realestate=b.id_realestate AND a.visitor=b.visitor
) LIMIT 1';
Not a very clear question but you can use a query like this: (untested)