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

  • SEARCH
  • Home
  • 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 8676833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:17:57+00:00 2026-06-12T20:17:57+00:00

I spent some time trying to get working this SELECT with CASE but I

  • 0

I spent some time trying to get working this SELECT with CASE but I failed… (thank to that I’m using COLASCE() now)

How could I optimize this SELECT by using CASE/IF sentences? Is this a fast way to query from different tables selected by a field?

SELECT a.folderid, a.foldername, a.contenttype, COALESCE(b.descriptor, c.descriptor, d.descriptor, e.descriptor, f.descriptor) as descriptor
FROM t_folders a
LEFT JOIN t_files b
ON a.contenttype = 'file' AND a.contentid = b.fileid
LEFT JOIN t_links c
ON a.contenttype = 'link' AND a.contentid = c.linkid
LEFT JOIN t_extfiles d
ON a.contenttype = 'extfile' AND a.contentid = d.extfileid
LEFT JOIN t_videos e
ON a.contenttype = 'video' AND a.contentid = e.videoid
LEFT JOIN t_exams f
ON a.contenttype = 'exam' AND a.contentid = f.examid
WHERE a.folderid = $folderId
ORDER BY a.folderid DESC
  • 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-12T20:17:58+00:00Added an answer on June 12, 2026 at 8:17 pm

    Using case statement will not make the query faster in your case, but since you asked for it, below is how it would look like.

    SELECT a.folderid, a.foldername, a.contenttype, 
        (CASE a.contenttype
            WHEN 'file' THEN b.descriptor
            WHEN 'link' THEN c.descriptor
            WHEN 'extfile' THEN d.descriptor
            WHEN 'video' THEN e.descriptor
            ELSE f.descriptor
        END CASE) AS descriptor
    FROM t_folders a
    LEFT JOIN t_files b ON a.contenttype = 'file' AND a.contentid = b.fileid
    LEFT JOIN t_links c ON a.contenttype = 'link' AND a.contentid = c.linkid
    LEFT JOIN t_extfiles d ON a.contenttype = 'extfile' AND a.contentid = d.extfileid
    LEFT JOIN t_videos e ON a.contenttype = 'video' AND a.contentid = e.videoid
    LEFT JOIN t_exams f ON a.contenttype = 'exam' AND a.contentid = f.examid
    WHERE a.folderid = $folderId
    ORDER BY a.folderid DESC
    

    If each of the t_files, t_links, etc tables has the folder_id field, I would also try doing a UNION on these tables and then left join the result with t_folders to get the folderid and foldername.

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

Sidebar

Related Questions

I've spent a long time trying different things to get this to work but
I spent some time trying to figure out why this query isn't pulling the
I've spent some time trying to find a way CodeRush could add using when
I spent some time logged into a server ( Debian ) trying to get
So I've spent some time looking for an answer for this, but so far
I spent some time trying to research a definitive answer for this and couldn't
I spent some time this morning trying to help a colleague with an odd
I recently spent some time trying to write some numbers as bytes to pipe
New to MVC3 Razor - linq to sql having spent some time trying to
I spent some time looking for a way of resolving my issue but with

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.