私たちが知っているように、LEFT JOIN SQL ステートメントは、LEFT テーブルからのすべての値と、Right テーブルからの ID (外部キー) を介したジョイント値を提供します。たとえば、次の表があります
comments
comment_id
parent_id
comment_body
このテーブルには次のデータがあります
-----------|-----------|-------------------|
comment_id | parent_id | comment_body |
-----------|-----------|-------------------|
1 | NULL | parent comment |
-----------|-----------|-------------------|
2 | 1 | child comment |
-----------|-----------|-------------------|
3 | NULL | parent comment |
-----------|-----------|-------------------|
4 | 1 | child comment |
-----------|-----------|-------------------|
5 | 3 | child comment |
-----------|-----------|-------------------|
6 | 1 | parent comment |
-----------|-----------|-------------------|
7 | NULL | parent comment |
-----------|-----------|-------------------|
8 | 1 | child comment |
-----------|-----------|-------------------|
9 | 7 | child comment |
-----------|-----------|-------------------|
10 | 3 | child comment |
-----------|-----------|-------------------|
たとえば、上記のテーブルで次のクエリを実行します
SELECT c1 . * , c2 . *
FROM comments c1
LEFT JOIN comments c2 ON c2.parent_id = c1.comment_id
WHERE c1.parent_id IS NULL
次の出力が得られます
-----------|-----------|---------------|------------|-----------|---------------|
comment_id | parent_id | comment_body | comment_id | parent_id | comment_body |
-----------|-----------|---------------|------------|-----------|---------------|
1 | NULL |parent comment | 1 | 1 | child comment |
-----------|-----------|---------------|------------|-----------|---------------|
1 | NULL |parent comment | 4 | 1 | child comment |
-----------|-----------|---------------|------------|-----------|---------------|
1 | NULL |parent comment | 6 | 1 | child comment |
-----------|-----------|---------------|------------|-----------|---------------|
1 | NULL |parent comment | 8 | 1 | child comment |
-----------|-----------|---------------|------------|-----------|---------------|
3 | NULL |parent comment | 5 | 3 | child comment |
-----------|-----------|---------------|------------|-----------|---------------|
3 | NULL |parent comment | 10 | 3 | child comment |
-----------|-----------|---------------|------------|-----------|---------------|
7 | NULL |parent comment | 9 | 7 | child comment |
-----------|-----------|---------------|------------|-----------|---------------|
ここで値を強調表示しました。これは余分であり、必要ありません。
そして私の意見では、これらの追加の値はパフォーマンスに効果的である可能性があります。次の形式で出力を取得できますか
それが不可能な場合は、上記のクエリの長所と短所を教えてください。また、使用する必要がありますか?