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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:26:28+00:00 2026-06-03T02:26:28+00:00

I have a prepared query in two versions, the first one works but the

  • 0

I have a prepared query in two versions, the first one works but the second using CASE WHEN doesn’t :

 SELECT  mFrom.user_name AS sender_name, mp.message AS message
 FROM msg_pv mp
 INNER JOIN membresmvc AS mFrom 
    ON mp.id_from = mFrom.id
 WHERE mp.id_to = :id_member

CASE WHEN version :

 SELECT  mFrom.user_name AS sender_name, mp.message AS message
 FROM msg_pv mp
 INNER JOIN 
    CASE mp.from_type
        WHEN "mb" THEN membresmvc AS mFrom ON mFrom.id = mp.id_from 
    END -- also tried "END CASE"    
 WHERE mp.id_to = :id_member

The value of mp.from_type is always “mb” in the msg_pv table. I exactly use the structure recommented in the MySQL reference manual. I don’t understand why the second query doesn’t work.

  • 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-03T02:26:29+00:00Added an answer on June 3, 2026 at 2:26 am

    If your CASE espression value always evaluates to true, there is no need to have that statement.

    However, I think the usage of the CASE in your query is wrong. It should be something like as shown below. You can have the CASE on the ON condition but not on the JOIN itself. This way you can join to different columns based on a condition.

    This is syntactically correct but not sure what the output would be.

    You can validate this statement using this link: Instant SQL Formatter

    Query:

    SELECT      mFrom.user_name AS sender_name, 
                mp.message      AS message 
    FROM        msg_pv mp 
    INNER JOIN  membresmvc 
    ON          ( 
                    CASE mp.from_type 
                        WHEN "mb" THEN mFrom.id = mp.id_from 
                        ELSE NULL 
                    END 
                ) 
    WHERE  mp.id_to = :id_member;
    

    UPDATE

    If you need to join to multiple tables based on a condition, then you need to make use of LEFT OUTER JOIN. You query might look something like this assuming your second table is *second_table* and another value is “another value”

    SELECT          COALESCE(mFrom.user_name, second_table.some_col) AS sender_name, 
                    mp.message      AS message 
    FROM            msg_pv mp 
    LEFT OUTER JOIN membresmvc 
    ON              mFrom.id        = mp.id_from 
    AND             mp.from_type    = "mb"
    LEFT OUTER JOIN second_table 
    ON              second_table.id = mp.id_from 
    AND             mp.from_type    = "another value"
    WHERE           mp.id_to = :id_member;
    

    EXAMPLE

    Here is an example that illustrates it. This script was tested in SQL Server 2012 database. It might differ in MySQL. However, the concept of LEFT OUTER JOIN used here is same even in MySQL.

    • There are three tables namely table1, table2 and table3
    • Here we have to join table1 with both table2 and table3 based on the below conditions.
    • Join colrefid in table1 with col2 in table2 if constant in table1 has the value t2.
    • Join colrefid in table1 with col3 in table3 if constant in table1 has the value t3.
    • You can see the output at the end

    Hope that gives you an idea to join the tables as per your requirements.

    Script:

    CREATE TABLE dbo.Table1
    (
            col1        INT         NOT NULL
        ,   colrefid    INT         NOT NULL
        ,   constant    VARCHAR(10) NOT NULL
    );
    
    CREATE TABLE dbo.Table2
    (
            col2    INT         NOT NULL
        ,   name    VARCHAR(10) NOT NULL
    );
    
    CREATE TABLE dbo.Table3
    (
            col3    INT         NOT NULL
        ,   name    VARCHAR(10) NOT NULL
    );
    
    INSERT INTO dbo.Table1 (col1, colrefid, constant) VALUES
        (123, 2, 't2'),
        (784, 3, 't3'),
        (498, 2, 't2');
    
    INSERT INTO dbo.Table2 (col2, name) VALUES
        (2, 'table 2');
    
    INSERT INTO dbo.Table3 (col3, name) VALUES
        (3, 'table 3');
    
    SELECT          t1.col1
                ,   t1.colrefid
                ,   t1.constant
                ,   COALESCE(t2.col2, t3.col3) colvalue
                ,   COALESCE(t2.name, t3.name) colname
    FROM            dbo.Table1  t1
    LEFT OUTER JOIN dbo.Table2  t2
    ON              t2.col2     = t1.colrefid
    AND             t1.constant = 't2'
    LEFT OUTER JOIN dbo.Table3  t3
    ON              t3.col3     = t1.colrefid
    AND             t1.constant = 't3';
    

    Output:

    col1 colrefid constant colvalue colname
    ---- -------- -------- -------- -------
    123      2       t2        2    table 2
    784      3       t3        3    table 3
    498      2       t2        2    table 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select query (using a prepared statement) which returns a Resultset. For
Can I have a prepared statement with the following query: select * from table
I have the following prepared query I am using with mysql and php. It
I have prepared a query but it keeps throwing errors. Here is my query:
I have prepared few queries using startwith and connectby for fetching all items of
I have prepared one sample application.When application install in devices download the file from
I have tried several things but I can't get the MySQL query in my
I have a case where I want to use the results of a prepared
I have done this before but am quite new to mysqli and prepared statements
I have a form which has two inputs. The first input allows a user

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.