1

整数を文字列にマップする辞書と、整数間の関係を格納する別のテーブルがあります。

    Dictionary (id, stringvalue)
    1      Where
    2      are you
    3      going
    4      This 
    5      is
    6      stackoverflow

   Table(col1, col2, col3)
   Col1    col2     col3
   1       2        3
   4       5        6

2 つのテーブルをクエリする手順は次のとおりです。

1. Given string map them to ids using dictionary
2. Given strings map them to corresponding column using Table, which gives an integer
3. Given ids which we got from step1 map them again to string and present the result to the user?

"Where are" の後の次の列の値を調べるために、次の形式でクエリを実行できます。

   select d3.stringvalue from dictionary d1, dictionary d2, dictionary d3, Table t1 where
   d1.stringvalue='Where' and d1.id=Table.Col1 and d2.stringvalue='are' and d2.id=Table.Col2 and d3.id=Table.Col3;

上記のクエリから期待される出力は次のとおりです。

      you

col1=1 ('Where')、Col2=2 ('Are') の場合、Col3 の文字列値 ('You') を知りたいですか?

ただし、より複雑なクエリの場合、この ID から文字列へのマッピングは面倒であることがわかります。SQLで同じクエリを読みやすく短いクエリに変換する方法はありますか

4

1 に答える 1