加入转换表,在SQLite中保留订单表单索引

时间:2012-06-10 16:19:24

标签: sql sqlite

我使用这个SQL来获取一些索引:

select follow 
from unigram 
where alternativeSpelling like 'test' 
order by freq desc 
limit 10;

然后我逐个使用这个SQL将它们转换为单词:

select word 
from wordIdxTranslate 
where word_idx = <one of the indexes from above>

如何将这些结合到单个查询中,从第一个查询(“freq”)中保留排名顺序?

2 个答案:

答案 0 :(得分:3)

未经测试,但应该这样做:

SELECT word 
FROM unigram, wordIdxTranslate
WHERE 
    unigram.follow=wordIdxTranslate.word_idx
    AND
    unigram.follow IN (SELECT T1.follow 
                       FROM unigram AS T1 
                       WHERE T1.alternativeSpelling LIKE 'test' 
                       ORDER BY T1.freq DESC 
                       LIMIT 10)
ORDER BY freq DESC

答案 1 :(得分:1)

一种选择是将查询与join结合起来,例如:

select  word 
from    (
        select  follow 
        ,       freq
        from    unigram 
        where   alternativeSpelling like 'test' 
        order by 
                freq desc 
        limit 10
        ) uni
join    wordIdxTranslate  wit
on      wit.word_idx = uni.follow
order by
        uni.freq desc