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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:19:52+00:00 2026-06-10T16:19:52+00:00

In the returned results on a query to a ‘Links’ table I want to

  • 0

In the returned results on a query to a ‘Links’ table I want to also add an extra column that shows a ‘DisplayName’ value that is selected from a JOIN conditional on the value of the ‘toTable’ per row. In addition sometimes that ‘DisplayName’ value is the result of a concatenation of columns.

My question is how do I do this?

I can write seperate queries and combine with UNION ALL though I wonder if there is a conditional way to do it

Each entry in the ‘Links’ table describes links between a pair of entries in two tables (of a possible 3 or more tables).

Links Table:

| LinkID | fromTable | fromID | toTable | toID |
|      1 |        A  |      1 |      B  |    1 |
|      2 |        A  |      1 |      C  |    2 |
|      3 |        B  |      1 |      C  |    1 |
|      4 |        C  |      1 |      A  |    1 |

Query results I want:
WHERE Links.fromTable = ‘A’ OR Links.fromTable = ‘C’ for example

| LinkID | fromTable | fromID | toTable | toID | DisplayName      |
|      1 |        A  |      1 |      B  |    1 |  Some Title      |
|      2 |        A  |      1 |      C  |    2 |  Loud-Quack      |
|      4 |        C  |      1 |      A  |    1 |  Linus           |

3 Tables with different Columns:

Table A:    
| ID  | Name  |
| 1   | Linus |

–

Table B:    
| ID  | Title      |
|  1  | some title |

–

Table C:    
| ID  | CategoryA | CategoryB |
| 1   |   Bark    |    Softly |
| 2   |   Quack   |    Loud   |

–
This is how I am doing this will UNION ALL which is not very flexible:
EDIT: actually i think the below is wrong, i am looking at this now:

SELECT Links.*, A.Name AS DisplayName
FROM Links 
JOIN A ON Links.toID = A.ID 
WHERE Links.fromType IN ('A') AND Links.toType IN ('B')

UNION ALL

SELECT Links.*, CONCAT(C.CategoryB,'-',C.CategoryA) AS DisplayName
FROM Links 
JOIN C ON Links.toID = C.ID 
WHERE Links.fromType IN ('A') AND Links.toType IN ('C')
  • 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-10T16:19:54+00:00Added an answer on June 10, 2026 at 4:19 pm

    I thin you need inline CASE statement

    SELECT  a.*,
            CASE toTable
                WHEN 'A' THEN b.Name
                WHEN 'B' THEN c.Title
                WHEN 'C' THEN CONCAT(d.CategoryB , '-',d.CategoryA )
            END AS DisplayName
    FROM    `links` a
                LEFT JOIN tableA b
                    ON a.toID = b.ID
                LEFT JOIN tableB c
                    ON a.toID = c.ID
                LEFT JOIN tableC d
                    ON a.toID = d.ID
    WHERE   fromtable IN ('A', 'C')
    

    SQLFiddle Demo

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

Sidebar

Related Questions

All I really want to know is whether or not my query returned results
What is the recommended idiom for checking whether a query returned any results? Example:
Im some array that's being returned by some query.. and the result is something
I have searched high and low for an answer to why query results returned
I simply want to include a row number against the returned results of my
How can I count the number of results returned by a group query without
I have a report window which shows the results returned from a potentially long
I have a query that will return results from 2 tables into 1 using
How do you remove and add elements to results returned by doctrine's odm. For
I am trying to restrict the results returned from a query. If 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.