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 6230853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:49:41+00:00 2026-05-24T09:49:41+00:00

As I am now aware, CASE can be used only in WHERE context. Though,

  • 0

As I am now aware, CASE can be used only in WHERE context. Though, I need to use different table depending on column value. What I’ve tried looks like this:

SELECT
    `ft1`.`task`,
    COUNT(`ft1`.`id`) `count`
FROM
    `feed_tasks` `ft1`
CASE
    `ft1`.`type`
WHEN
    1
THEN
    (INNER JOIN `pages` `p1` ON `p1`.`id` = `ft1`.`reference_id`)
WHEN
    2
THEN
    (INNER JOIN `urls` `u1` ON `u1`.`id` = `ft1`.`reference_id`)
WHERE
    `ft1`.`account_id` IS NOT NULL AND
    `a1`.`user_id` = {$db->quote($user['id'])}

Now that I know this is invalid syntax, what’s the closest alternative?

  • 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-05-24T09:49:41+00:00Added an answer on May 24, 2026 at 9:49 am

    It probably needs tweaking to return the correct results but I hope you get the idea:

    SELECT ft1.task, COUNT(ft1.id) AS count
    FROM feed_tasks ft1
    LEFT JOIN pages p1 ON ft1.type=1 AND p1.id = ft1.reference_id
    LEFT JOIN urls u1 ON ft1.type=2 AND u1.id = ft1.reference_id
    WHERE COALESCE(p1.id, u1.id) IS NOT NULL
    AND ft1.account_id IS NOT NULL
    AND a1.user_id = :user_id
    

    Edit:

    A little note about CASE...END. Your original code does not run because, unlike PHP or JavaScript, the SQL CASE is not a flow control structure that allows to choose which part of the code will run. Instead, it returns an expression. So you can do this:

    SELECT CASE
        WHEN foo<0 THEN 'Yes'
        ELSE 'No'
    END AS is_negative
    FROM bar
    

    … but not this:

    -- Invalid
    CASE 
        WHEN foo<0 THEN SELECT 'Yes' AS is_negative
        ELSE SELECT 'No' AS is_negative
    END
    FROM bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I am aware that I probably need to use a ListBox and can
Now I'm sure we're all well aware of the relative merits of Linux vs
I'm aware of server-side javascript for a long time now, but I don't have
now i need to insert some data from the sqlserver into a word,i know
I used to use OpenC++ ( http://opencxx.sourceforge.net/opencxx/html/overview.html ) to perform code generation like: Source:
So, I use jQuery quite extensively and I am well aware of the right
I am aware that Dijkstra's algorithm can find the minimum distance between two nodes
Edit: I'm well aware of that this works very well with value types, my
I'm a little confused on Xlib programming now. I started to use dwm (a
Did you ever have a day when nothing works? Now I can't even succeed

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.