SQL子查询和连接

时间:2013-02-01 02:56:33

标签: mysql sql select subquery

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)

我不想在那里同时拥有176的ID,我想要最近添加的一个(这就是为什么我在我的subquerys上使用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)

2 个答案:

答案 0 :(得分:2)

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
        INNER JOIN support 
            ON session.session_id = support.session_id
        INNER 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 NOT IN (0,2);

答案 1 :(得分:1)

这是使用子查询的替代方法。你可以再次加入桌面。像这样:

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
        INNER 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 NOT IN (0,2) 
    AND support2.session_id IS NULL

两种方法都应该产生相同的结果。

祝你好运。