0
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を設定したくないので、最後に追加したIDが必要です(そのため、サブクエリで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)
4

2 に答える 2

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);
于 2013-02-01T02:59:49.540 に答える
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

どちらのアプローチでも同じ結果が得られるはずです。

幸運を。

于 2013-02-01T03:16:42.930 に答える