We have three tables users,answers and stretches. When someone login to our system we stored user_id,created_at and updated_at in stretches table and when he/she logout updated deleted at in the same row.
Session time 30 minutes.
users table
- id
- f_name
- l_name
answers table
- id
- user_id
- body
- created_at
- updated_at
stretches table
- id
- user_id
- created_at
- updated_at
- deleted_at
Now i want to get only those rows from the stretches table where an answer has been given. I wrote this query but it takes to much time but did not return accurate data.
$query = "select count(a.id), a.user_id
from answers a
where a.created_at in
(select s.created_at
from stretches s
where s.created_at >= a.created_at
) group by a.user_id";
$results = DB::select(DB::raw($query));
I'd be curious to hear how some of you write this query.
Answers table
id | body | user_id | created_at | updated_at
10 | text | 10 | 2015-10-10 0:0:0 | 2015-10-10 0:0:0
Stretches Table
id | user_id | created_at | updated_at | deleted_at
1 | 10 | 2015-10-00 0:0:0 | 2015-10-10 0:0:0 | 2015-10-20 0:0:0
Now i want to get those rows from stretches table where an answer has been given.
Query results should be:
id | user_id | created_at | updated_at | deleted_at
1 | 10 | 2015-10-10 0:0:0 |2015-10-10 0:0:0 | 2015-10-10 0:0:0
answersandstretchestable and you expected output based on that?