SQL查询表2x然后将结果合并为1列

时间:2014-10-01 19:33:45

标签: sql sqlite

我正在使用sqlite并拥有语言词典。我想用英语搜索一次表格,然后再次通过匹配外键(日语字符)找到另一种语言的结果。

目前,我可以通过2个查询和4个单独的列来获得结果:text x2和language x2。

如何将它们组合成一列作为新行?我不认为SQL的联合是我正在寻找的。 我也尝试在select中进行选择以查询表两次,但我的笔记本电脑上的查询时间从~700ms到~1400ms我计划在移动应用中使用它,所以我想保留减速。我也希望使用cshart sqlite ORM来获取这些结果并将它们放入具有语言集合的模型中。

这是我尝试过的一个示例查询(我试过一堆)

-- find the Character using the Reading's ChFKEY
select
    r.ID,
    r.Text,
    r.Language,
    reading.Text,
    rl.Language,
    r.ChID,
    r.KaID
from

-- first, find a dictionary match for English
(select
        r.ID,
        r.Text,
        rl.Language,
        r.ChID,
        r.KaID
    from Reading r
    join ReadingLanguage rl on r.RLID = rl.ID
    where r.RLID = 1
    and r.Text like 'food%')r
join Reading reading on reading.RLID in (8,9) -- find JapaneseOn, JapaneseKun
join ReadingLanguage rl on reading.RLID = rl.ID  --get the language text
and reading.ChID = r.ChID; -- use the character FKey to find matches

以下是上述查询的示例结果:
“977”“食物,诱饵,猎物,诱人的利润”“英语”“ジ,ニ”“JapaneseOn”“284”“128”
“977”“食物,诱饵,猎物,诱人的利润”“英语”“え,えば,えさ,もち”“JapaneseKun”“284”“128”

修改
这是我希望得到的:
“977”“食物,诱饵,猎物,诱人的利润”“英语”“284”“128”
“977”“え,えば,えさ,もち”“JapaneseKun”“284”“128”
“977”“ジ,ニ”“JapaneseOn”“284”“128”

1 个答案:

答案 0 :(得分:0)

编辑我突然意识到,我可以完成整个过程来制作我想要的字典结果,并且我正在以错误的方式思考它。我现在使用group_concat将日语结果放在一起,并将英语放在另一列中。这样,c#中不需要工作来生成视图的结果。

    select
ID,
Text as English,
group_concat(Japanese) as Japanese,
Character
from
(
select 
    r.ID,
    r.Text,
    ch.Text as Character,
    reading.Text as Japanese
from
(
    (
    select
        r.ID,
        r.Text,
        rl.Language,
        r.ChID,
        rl.ID
    from Reading r
    join ReadingLanguage rl on r.RLID = rl.ID
    where r.RLID = 1 --English
    and r.Text like 'food%'
    )r
join Reading reading on reading.RLID in (8,9) --JapaneseOn, JapaneseKun
join ReadingLanguage rl on reading.RLID = rl.ID 
join Character ch on r.ChID = ch.ID --grab the kanji character
and reading.ChID = r.ChID
)
order by rl.ID desc --so that the hiragana shows up before the katakana
) 
group by Character;

新结果是 " 34905" "食物,食物和#34; "かて,リョウ,ロウ" "粮"

编辑旧版 我得到了一些帮助。事实证明,Union All是实现这一目标的方法。新查询在446ms内完成(除非我订购)并且我希望ORM将它们正确地放入其父对象中。

-- find the Character using the Reading's ChFKEY
-- 448 ms

select
    r.ID,
    r.Text,
    rl.Language,
    ch.Text as Character
from Reading r
join ReadingLanguage rl on r.RLID = rl.ID
join Character ch on r.ChID = ch.ID
where r.RLID = 1
and r.Text like 'food%'

union all

select
    r.ID,
    reading.Text,
    rl.Language,
    ch.Text as Character
from
(
    select
        r.ID,
        r.Text,
        rl.Language,
        r.ChID,
        r.KaID
    from Reading r
    join ReadingLanguage rl on r.RLID = rl.ID
    where r.RLID = 1
    and r.Text like 'food%')r
join Reading reading on reading.RLID in (8,9) -- find the JapaneseOn, JapnaeseKun
join ReadingLanguage rl on reading.RLID = rl.ID -- get the actual language
join Character ch on r.ChID = ch.ID -- get the japanese character that it belongs to
and reading.ChID = r.ChID
--order by r.ID
;

,样本输出为
" 977" "食物,诱饵,猎物,诱人的利润" "英语" "饵"
" 977" "ジ,ニ" " JapaneseOn" "饵"
" 977" "え,えば,えさ,もち" " JapaneseKun" "饵"