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);
这会返回我这个值:
+-----+-----------+---------+---------+--------------+-----------------+---------+-------------------+
| 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)
我不想在那里有两个 ID 176,我想要最近添加的一个(这就是为什么我在我的子查询中使用 order by starttime asc limit 1)。有什么办法可以让我用我现在提出的方法来解决这个问题?
编辑编号 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)
也没有达到预期的效果吗?我的 support.starttime 是否应该是数据库的索引,因此 timing.starttime = support.starttime 会起作用?
编辑 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)
编辑 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)
编辑:现在可以使用,感谢 JQ 的帮助!
并感谢 sgeddes 的回答!
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)
编辑 4:不更改表格,当我运行报告时,我只有一个可用的历史视图。然后我现在需要一个最新的当前视图。
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)