Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9049705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:28:00+00:00 2026-06-16T12:28:00+00:00

I want to create an SQL statement (using sqllite.. so I can’t use functions,

  • 0

I want to create an SQL statement (using sqllite.. so I can’t use functions, IF statements etc..) that reuses the result of a select statement.. this is what it currently looks like

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
)

I want to reuse the result of first select instead of having to make another (almost identical) select statement.

update: for those wondering what i’m trying to do.. here is the full version of the query:

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
)

basically i’m trying to find out if an email thread already exists for an incoming email.. so i check if the subject matches and if it does, i check to see if the sender and receiver of the email match (in either direction).. if the email thread exists i just insert the same email threadID, else i generate a new threadID

update 2:just to clarify, I’m looking for a way to save the sqllite compiler from making the same search twice.. rather than simply saving on typing (or making it more readable etc)

update 3: i was wondering if there was a way for this statement to return the generated threadID iff the threadID was retrieved from the dbase, rather than generated.. you can find the answer here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:28:02+00:00Added an answer on June 16, 2026 at 12:28 pm

    This is an alternative way of structuring the query:

    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>
                        )
    

    This is using a select instead of values. It gets the thread id that matches the conditions, or NULL if none match. The second clause of the coalesce is then run when the first is NULL. You can generate the new id there.

    I do have a problem with this approach. To me, it seems that you should have a Thread table that manages the threads. The ThreadId should be an autoincremented id in this table. The emails table can then reference this id. In other words, I think the data model needs to be thought out in more detail.

    The following query will not query work, but it gives the idea of moving the thread to the subquery:

    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
    

    The reason it will not work is because the from clause will return no rows rather than 1 row with a NULL value. So, to get what you want, you can use:

        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
    

    This ensures that a NULL value is returned when there is no thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create the following T-SQL statement: SELECT SUM (sa.Amount) as 'SumAmount', SUM(sa.Cost)
I've a query that creates a SQL Statement as a field. I want to
I want to create an SQL query that selects the pictures of females in
I want to create an SQL script that creates a database. Right now, I
I want to create an SQL database for a phonegap application that i am
I want to create a stored procedure (on SQL Server 2005) that fetches a
How do I create a temporary result set for use in an SQL without
In PostgreSQL, I want to use an SQL statement to combine two columns and
I want to create a view for this sql statement which previously works fine.
In MySQL, we can generate the prepared statement using PreparedStatement . I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.