我有一个字典,它将整数映射到字符串和另一个存储整数之间关系的表:
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
查询这两个表的步骤如下:
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 中简洁易读且简短的查询