我如何合并连接?

时间:2017-11-17 13:08:49

标签: mysql join left-join

我有以下查询,但我想改进(性能明智):

sentence_id和。project_id是索引。

我现在的目标是尝试将同一个表上的2个连接合并为一个(如果可能的话) 我怎么能这样做?

SELECT rs.project_id,
               ms.id AS raw_sentence_id,
               rs.sentence AS raw_sentence_text,
               rs.sentence_id AS raw_sentence_text_id,
               rst.tokens AS tokenization_information,
               ns.sentence_id AS normalized_sentence_id,
               ns.sentence AS normalized_sentence_text,
               nst.token_indices
          FROM manual_sentences ms
          JOIN sentences rs ON (ms.original_sentence_id = rs.sentence_id AND rs.project_id = 623483)
     LEFT JOIN sentence_tokens rst ON (ms.original_sentence_id = rst.sentence_id AND rst.project_id = 623483)
          JOIN sentences ns ON (ms.sentence_id = ns.sentence_id AND ns.project_id = 623483)
     LEFT JOIN sentence_tokens nst ON (ms.sentence_id = nst.sentence_id AND nst.project_id = 623483)
     WHERE ms.project_id = 623483

1 个答案:

答案 0 :(得分:0)

你应该能够使用连词来做到这一点:

SELECT rs.project_id,
               ms.id AS raw_sentence_id,
               rs.sentence AS raw_sentence_text,
               rs.sentence_id AS raw_sentence_text_id,
               rst.tokens AS tokenization_information,
               ns.sentence_id AS normalized_sentence_id,
               ns.sentence AS normalized_sentence_text,
               nst.token_indices
          FROM manual_sentences ms
          JOIN sentences rs ON (ms.original_sentence_id = rs.sentence_id AND rs.project_id = 623483)
     LEFT JOIN sentence_tokens rst ON (ms.original_sentence_id = rst.sentence_id AND ms.sentence_id = rst.sentence_id AND rst.project_id = 623483)
          JOIN sentences ns ON (ms.sentence_id = ns.sentence_id AND ns.project_id = 623483)
     WHERE ms.project_id = 623483