在CASE语句sqlite中重用SELECT语句的结果

时间:2012-12-27 14:20:40

标签: sql sql-server sqlite

我想创建一个SQL语句(使用sqllite ..所以我不能使用函数,IF语句等..)重用select语句的结果..这就是它目前的样子

INSERT INTO search_email(many_fields, threadid) VALUES ('many_fields',
    CASE 
    WHEN
            (
                (SELECT COUNT(tableX.threadid) %threadIDquery% 
                ) > 0
            )
    THEN

                (SELECT tableX.threadid %threadIDquery% LIMIT 1)

    ELSE 
            0
    END
)

我想重用第一个select的结果,而不是必须创建另一个(几乎相同的)select语句。

更新:对于那些想知道我正在尝试做什么的人..这里是查询的完整版本:

INSERT INTO search_email(meta, subject, body, sender, tos, ccs, folder, threadid) VALUES ('meta1','subject1','body1','sender1', 'tos1',' ccs1','folder1',
    CASE 
    WHEN
            (
                (SELECT COUNT(search_email.threadID) FROM search_email 
                                                            WHERE search_email.subject MATCH '%query%'  AND 
                                                            (
                                                                (search_email.sender = '%sender' AND search_email.tos = '%receiver%')
                                                                OR 
                                                                (search_email.sender = '%receiver%' AND search_email.tos = '%sender%')
                                                            )
                ) > 0
            )
    THEN

            (SELECT search_email.threadID FROM search_email 
                                                        WHERE search_email.subject MATCH '%query%'  AND 
                                                        (
                                                            (search_email.sender = '%sender%' AND search_email.tos = '%receiver%')
                                                            OR 
                                                            (search_email.sender = '%receiver%' AND search_email.tos = '%sender%')
                                                        )  LIMIT 1
            )

    ELSE 
            //generate new thread ID
    END
)

基本上我试图找出是否已经存在传入电子邮件的电子邮件线程..所以我检查主题是否匹配,如果是,我检查电子邮件的发件人和收件人是否匹配(在任何方向)..如果电子邮件线程存在我只插入相同的电子邮件threadID,否则我生成一个新的threadID

更新2:只是为了澄清,我正在寻找一种方法来保存sqllite编译器两次进行相同的搜索...而不是简单地保存打字(或使其更具可读性等) )

更新3:我想知道这个语句是否有办法返回生成的threadID iff从dbase检索threadID而不是生成..你可以找到答案{{ 3}}

1 个答案:

答案 0 :(得分:3)

这是构建查询的另一种方法:

INSERT INTO search_email(meta, subject, body, sender, tos, ccs, folder, threadid)
    SELECT 'meta1', 'subject1', 'body1', 'sender1', 'tos1', 'ccs1', 'folder1',
            coalesce((SELECT search_email.threadID
                      FROM search_email 
                      WHERE search_email.subject MATCH '%query%' AND 
                            ((search_email.sender = '%sender%' AND search_email.tos = '%receiver%') OR
                             (search_email.sender = '%receiver%' AND search_email.tos = '%sender%')
                            )
                      LIMIT 1
                     ),
                     <generate new thread id here>
                    )

这是使用select而不是values。它获取与条件匹配的线程id,如果没有匹配则获取NULL。然后在第一个为NULL时运行coalesce的第二个子句。你可以在那里生成新的id。

我确实遇到过这种方法的问题。对我来说,似乎你应该有一个管理线程的Thread表。 ThreadId应该是此表中的自动增量ID。然后,电子邮件表可以引用此ID。换句话说,我认为需要更详细地考虑数据模型。

以下查询不会查询工作,但它提供了将线程移动到子查询的想法:

INSERT INTO search_email(meta, subject, body, sender, tos, ccs, folder, threadid)
    SELECT 'meta1', 'subject1', 'body1', 'sender1', 'tos1', 'ccs1', 'folder1',
            coalesce(t.threadID,
                     <generate new thread id here>
                    )
    from (SELECT search_email.threadID
          FROM search_email 
          WHERE search_email.subject MATCH '%query%' AND 
                ((search_email.sender = '%sender%' AND search_email.tos = '%receiver%') OR
                 (search_email.sender = '%receiver%' AND search_email.tos = '%sender%')
                )
          LIMIT 1
         ) t

它不起作用的原因是因为from子句将不返回任何行,而不返回具有NULL值的1行。所以,为了得到你想要的东西,你可以使用:

    from (SELECT search_email.threadID
          FROM search_email 
          WHERE search_email.subject MATCH '%query%' AND 
                ((search_email.sender = '%sender%' AND search_email.tos = '%receiver%') OR
                 (search_email.sender = '%receiver%' AND search_email.tos = '%sender%')
                )
          union all
          select NULL
          order by (case when threadId is not null then 1 else 0 end) desc
          LIMIT 1
         ) t

这确保在没有线程时返回NULL值。