SELECT
session.session_id as ID,
session.anum,
student.first,
student.last,
session.why,
session.studentcomments,
session.aidyear,
support.counselorcomments
FROM student
INNER JOIN session ON student.anum = session.anum
LEFT JOIN support ON session.session_id = support.session_id
LEFT JOIN (
SELECT session_id, cid, starttime FROM support ORDER BY starttime asc limit 1
)
AS timing ON timing.session_id = session.session_id
WHERE session.status NOT IN (0,2);
This returns me with this value :
+-----+-----------+---------+---------+--------------+-----------------+---------+-------------------+
| ID | anum | first | last | why | studentcomments | aidyear | counselorcomments |
+-----+-----------+---------+---------+--------------+-----------------+---------+-------------------+
| 175 | A00000000 | rixhers | ajazi | Appeal | | 12-13 | NULL |
| 176 | B00000000 | Testing | thisout | Tap Question | | 12-13 | omg! |
| 176 | B00000000 | Testing | thisout | Tap Question | | 12-13 | Jesus!! |
+-----+-----------+---------+---------+--------------+-----------------+---------+-------------------+
3 rows in set (0.00 sec)
I do not want to have both ID’s of 176 there, I want the most recently added one (that is why i use order by starttime asc limit 1 on my subquerys). Is there any way I can get this to work with the means that I have set forward now?
Edit Number 1 :
mysql> SELECT session.session_id as ID,
-> session.anum,
-> student.first,
-> student.last,
-> session.why,
-> session.studentcomments,
-> session.aidyear,
-> support.counselorcomments
-> FROM student
-> INNER JOIN session
-> ON student.anum = session.anum
-> LEFT JOIN support
-> ON session.session_id = support.session_id
-> LEFT JOIN
-> (
-> SELECT session_id, max(starttime) max_time
-> FROM support
-> GROUP BY session_id
-> ) timing ON timing.session_id = support.session_id AND
-> timing.max_time = support.starttime
-> WHERE session.status IN (0,2);
+-----+-----------+---------+---------+--------------+-----------------+---------+-------------------+
| ID | anum | first | last | why | studentcomments | aidyear | counselorcomments |
+-----+-----------+---------+---------+--------------+-----------------+---------+-------------------+
| 175 | A00000000 | rixhers | ajazi | Appeal | | 12-13 | NULL |
| 176 | B00000000 | Testing | thisout | Tap Question | | 12-13 | omg! |
| 176 | B00000000 | Testing | thisout | Tap Question | | 12-13 | Jesus!! |
+-----+-----------+---------+---------+--------------+-----------------+---------+-------------------+
3 rows in set (0.00 sec)
Did not have the desired effect either anything else? Should my support.starttime be a index the database so timing.starttime = support.starttime will work?
Edit 2 :
mysql> SELECT session_id,
-> max(starttime) max_time FROM support
-> GROUP BY session_id;
+------------+---------------------+
| session_id | max_time |
+------------+---------------------+
| 176 | 2013-01-28 07:44:17 |
+------------+---------------------+
1 row in set (0.00 sec)
Edit 3 :
mysql> SELECT session_id, cid, starttime FROM support;
+------------+-----+---------------------+
| session_id | cid | starttime |
+------------+-----+---------------------+
| 176 | 1 | 2013-01-28 07:44:08 |
| 176 | 2 | 2013-01-28 07:44:17 |
+------------+-----+---------------------+
2 rows in set (0.00 sec)
EDIT : works now, thank you JQ for trying to help!
and thank you to sgeddes for the answer!
mysql> SELECT DISTINCT session.session_id as id,
-> session.anum,
-> student.first,
-> student.last,
-> session.why,
-> session.studentcomments,
-> session.aidyear,
-> support.counselorcomments
-> FROM student
-> INNER JOIN session
-> ON student.anum = session.anum
-> LEFT JOIN support
-> ON session.session_id = support.session_id
-> LEFT JOIN support support2
-> ON support.session_id = support2.session_id
-> AND support.starttime < support2.starttime
-> WHERE session.status IN (0,2)
-> AND support2.session_id IS NULL;
+-----+-----------+---------+---------+--------------+-----------------+---------+--------------------+
| id | anum | first | last | why | studentcomments | aidyear | counselorcomments |
+-----+-----------+---------+---------+--------------+-----------------+---------+--------------------+
| 175 | A00000000 | rixhers | ajazi | Appeal | | 12-13 | yo please help me! |
| 176 | B00000000 | Testing | thisout | Tap Question | | 12-13 | Jesus!! |
| 177 | C00000000 | ufufu | ufufjn | Appeal | | 12-13 | NULL |
+-----+-----------+---------+---------+--------------+-----------------+---------+--------------------+
3 rows in set (0.00 sec)
Edit 4: doesnt change the table, I just have a historic view available for when i am running reports. and then I needed a up to date current view for now.
mysql> SELECT session_id, cid, counselorcomments starttime FROM support;
+------------+-----+-------------------+
| session_id | cid | starttime |
+------------+-----+-------------------+
| 175 | 4 | NULL |
| 175 | 5 | help! |
| 175 | 7 | please need help! |
| 176 | 1 | omg! |
| 176 | 2 | Jesus!! |
| 177 | 1 | ihgggwhb |
+------------+-----+-------------------+
6 rows in set (0.00 sec)
Here’s an alternative to using a subquery. You could just join on the table again. Something like this:
Both approaches should produce the same results.
Good luck.