Mysql 如何在sql中编写子查询以映射字典值

Mysql 如何在sql中编写子查询以映射字典值,mysql,sql,oracle,postgresql,Mysql,Sql,Oracle,Postgresql,我有一个字典,它将整数映射到字符串,还有一个表存储整数之间的关系: 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

我有一个字典,它将整数映射到字符串,还有一个表存储整数之间的关系:

    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 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中简洁易读的简短查询

 select SUBSTRING(d2.stringvalue,5,7) AS "YOU" 
   from dictionary d1, dictionary d2, dictionary d3, Table t1
  where d1.stringvalue='Where' and
        d1.id=Table.Col1 and 
        SUBSTRING(d2.stringvalue,,3) ='are' and
        SUBSTRING(d2.stringvalue,5,7) ='you' and
        d2.id=Table.Col2 and 
        d3.id=Table.Col3;
如果您只需要“you”作为输出,那么上面的查询应该可以工作


如果您只需要“you”作为输出,那么上面的查询应该可以工作

你还可以清楚地阐述你的问题,并发布一些你期望的示例输出吗?@SOaddict我期望从上面的查询中得到的输出是“你”@SOaddict我已经试着让我的问题更清楚..我希望它能有所帮助。谢谢你的回答。你还可以清楚地阐述你的问题,并发布一些你所期望的示例输出吗?@SOaddict我期望从上面的查询中得到的输出是“你”@SOaddict我已经试着让我的问题更清楚了。我希望这会有所帮助。谢谢你的回复