2

私はこのコードを得ました:

        SELECT
            topics.id,
            topics.id_first,
                            posts.id_first
          FROM topics
        LEFT
          JOIN posts
            ON posts.id = topics.id_first_msg

私の意図は、次のようなことをすることです:

        SELECT
            topics.id,
            topics.id_first,
                            posts.id_first,
                            posts.id_last
          FROM topics
        LEFT
          JOIN posts
            ON posts.id = topics.id_first_msg
        LEFT
          JOIN posts
            ON posts.id = topics.id_last_msg

しかし、Left Join を 2 回実行しようとすると、エラーが発生します。では、どの方法が正しいのでしょうか?ありがとう。

4

3 に答える 3

4

複数回参加するテーブルのエイリアスを提供する必要があります。

SELECT
    topics.id,
    topics.id_first,
    p1.id_first,
    p2.id_last
FROM topics
LEFT JOIN posts p1 ON p1.id = topics.id_first_msg
LEFT JOIN posts p2 ON p2.id = topics.id_last_msg
于 2013-09-10T12:57:45.467 に答える
1

2 番目の左結合をエイリアスする必要があります。

    SELECT
        topics.id,
        topics.id_first,
                        posts.id_first,
                        posts2.id_last
      FROM topics
    LEFT
      JOIN posts
        ON posts.id = topics.id_first_msg
    LEFT
      JOIN posts AS posts2
        ON posts2.id = topics.id_last_msg
于 2013-09-10T12:57:47.660 に答える
0

投稿テーブルがあいまいです。最初に - 本当に 2 回参加する必要がありますか? これを行うことはできません:

    SELECT
        topics.id,
        topics.id_first,
        posts.id_first,
        posts.id_last,
        posts.id_first,
        posts.id_last
      FROM topics
    LEFT
      JOIN posts
        ON posts.id = topics.id_first_msg

2 つ目 - 本当に 2 回参加する必要がある場合 - posts テーブルに 2 つの異なるエイリアスを指定します (選択した列を編集する必要があります)。

于 2013-09-10T12:58:40.987 に答える