我有一张桌子,我正在尝试运行报告。问题是当使用相同的session_id
时,它只选择所有记录的第一个时间戳。
这是我的结果集:
+------------+-----------+---------+---------+------------------+---------------+----------+---------------------+----------+
| session_id | anum | first | last | counselor | why | start | Time With Counselor | total |
+------------+-----------+---------+---------+------------------+---------------+----------+---------------------+----------+
| 215 | A00000000 | rixhers | jdjdjdh | Christine McGraw | Appeal | 00:02:20 | 00:00:04 | 00:02:24 |
| 216 | B00000000 | rixhers | jdjdjdh | Dawn Lowe | Loan Question | 00:00:05 | 00:00:03 | 00:00:08 |
| 217 | D00000000 | forthis | isatest | Cherie McMickle | Loan Question | 00:02:08 | 00:00:02 | 00:02:10 |
| 218 | A00000000 | rixhers | jdjdjdh | John Ivankovic | Tap Question | 00:00:42 | 00:00:01 | 00:00:43 |
| 218 | A00000000 | rixhers | jdjdjdh | Christine McGraw | Tap Question | 00:00:42 | 00:00:01 | 00:00:43 |
| 218 | A00000000 | rixhers | jdjdjdh | Tootie | Tap Question | 00:00:42 | 00:00:01 | 00:00:43 |
| 218 | A00000000 | rixhers | jdjdjdh | Front-Kiana | Tap Question | 00:00:42 | 00:00:01 | 00:00:43 |
+------------+-----------+---------+---------+------------------+---------------+----------+---------------------+----------+
7 rows in set (0.00 sec)
请注意session_id
218
对所有记录都有相同的时间戳。
我按主键(session_id
,Counselor
分组),因为每位顾问可以在一个会话中工作,因此所有人的时间戳必须不同。
这是我的疑问:
SELECT
session.session_id,
session.anum,
student.first,
student.last,
c.counselor,
session.why,
SEC_TO_TIME(TIMEDIFF(t.start, session.signintime)) as start,
SEC_TO_TIME(TIMEDIFF(t.fin, t.start)) AS 'Time With Counselor',
SEC_TO_TIME(TIMEDIFF(t.fin, session.signintime)) AS total
FROM session
INNER JOIN student
ON student.anum = session.anum
LEFT JOIN (SELECT support.session_id, support.starttime AS start, support.finishtime AS fin FROM support GROUP BY support.session_id, support.cid) AS t
ON t.session_id = session.session_id
INNER JOIN (select support.session_id, support.cid, counselors.counselor FROM support INNER JOIN counselors ON counselors.cid = support.cid group by support.session_id, support.cid) AS c
ON c.session_id = session.session_id
WHERE session.status = 3
GROUP BY c.session_id, c.cid;
我在这里找不到简单的男人/女孩吗?
谢谢, -RaGe
编辑编号1:
mysql> SELECT * from support WHERE session_id = 218;
+------------+-----+---------------------+---------------------+-------------------+
| session_id | cid | starttime | finishtime | counselorcomments |
+------------+-----+---------------------+---------------------+-------------------+
| 218 | 1 | 2013-02-06 13:26:40 | 2013-02-06 13:26:41 | |
| 218 | 2 | 2013-02-06 13:26:45 | 2013-02-06 13:26:48 | done |
| 218 | 5 | 2013-02-06 13:26:25 | 2013-02-06 13:26:28 | v |
| 218 | 8 | 2013-02-06 13:26:34 | 2013-02-06 13:26:36 | |
+------------+-----+---------------------+---------------------+-------------------+
4 rows in set (0.00 sec)
编辑编号2:
mysql> SELECT * FROM session;
+------------+-----------+---------------+---------+---------------------+-----------------+--------+
| session_id | anum | why | aidyear | signintime | studentcomments | status |
+------------+-----------+---------------+---------+---------------------+-----------------+--------+
| 215 | A00000000 | Appeal | 12-13 | 2013-02-06 09:01:45 | | 3 |
| 216 | B00000000 | Loan Question | 12-13 | 2013-02-06 09:14:10 | | 3 |
| 217 | D00000000 | Loan Question | 12-13 | 2013-02-06 09:14:57 | | 3 |
| 218 | A00000000 | Tap Question | 12-13 | 2013-02-06 13:25:58 | | 3 |
+------------+-----------+---------------+---------+---------------------+-----------------+--------+
4 rows in set (0.00 sec)
如果需要,一个会话有很多支持票。这也是图片Of My schema
编辑3:
SELECT
s.session_id,
s.anum,
st.first,
st.last,
c.counselor,
s.why,
SEC_TO_TIME(TIMEDIFF(sup.starttime, s.signintime)) as start,
SEC_TO_TIME(TIMEDIFF(sup.finishtime, sup.starttime)) AS 'Time With Counselor',
SEC_TO_TIME(TIMEDIFF(sup.finishtime, s.signintime)) AS total
FROM session s
INNER JOIN student st
ON st.anum = s.anum
INNER JOIN support sup
ON s.session_id = sup.session_id
INNER JOIN counselors c
ON sup.cid = c.cid
WHERE s.status = 3
ORDER BY s.session_id asc;
+------------+-----------+---------+---------+------------------+---------------+----------+---------------------+----------+
| session_id | anum | first | last | counselor | why | start | Time With Counselor | total |
+------------+-----------+---------+---------+------------------+---------------+----------+---------------------+----------+
| 215 | A00000000 | rixhers | jdjdjdh | Christine McGraw | Appeal | 00:02:20 | 00:00:04 | 00:02:24 |
| 216 | B00000000 | rixhers | jdjdjdh | Dawn Lowe | Loan Question | 00:00:05 | 00:00:03 | 00:00:08 |
| 217 | D00000000 | forthis | isatest | Cherie McMickle | Loan Question | 00:02:08 | 00:00:02 | 00:02:10 |
| 218 | A00000000 | rixhers | jdjdjdh | Tootie | Tap Question | 00:00:27 | 00:00:03 | 00:00:30 |
| 218 | A00000000 | rixhers | jdjdjdh | Front-Kiana | Tap Question | 00:00:36 | 00:00:02 | 00:00:38 |
| 218 | A00000000 | rixhers | jdjdjdh | John Ivankovic | Tap Question | 00:00:42 | 00:00:01 | 00:00:43 |
| 218 | A00000000 | rixhers | jdjdjdh | Christine McGraw | Tap Question | 00:00:47 | 00:00:03 | 00:00:50 |
+------------+-----------+---------+---------+------------------+---------------+----------+---------------------+----------+
7 rows in set (0.00 sec)
答案 0 :(得分:1)
尝试运行此查询后,删除了使查询看起来像热门野兽的所有“善良”,并将其归结为您实际需要的所有内容。我建议将列命名为合适的。
SELECT
s.session_id,
s.anum,
st.first,
st.last,
c.counselor,
s.why,
SEC_TO_TIME(TIMEDIFF(sup.starttime, s.signintime)) as start,
SEC_TO_TIME(TIMEDIFF(sup.finishtime, sup.starttime)) AS 'Time With Counselor',
SEC_TO_TIME(TIMEDIFF(sup.finishtime, s.signintime)) AS total
FROM session s
INNER JOIN student st
ON st.anum = s.anum
INNER JOIN Support sup
ON s.session_id = sup.session_id
INNER JOIN Counselors c
ON sup.cid = c.cid
WHERE s.status = 3