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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:39:10+00:00 2026-06-06T05:39:10+00:00

I need to create dynamic INNER JOIN (I believe I need to anyway). I

  • 0

I need to create dynamic INNER JOIN (I believe I need to anyway). I have 4 tables pears, apples, oranges, bananas. I used all the same column names in those 4 tables.

Another 2 tables ‘fruit’ and ‘fruitcomments’

pears    apples    oranges   bananas
-------------------------------------
id
user
photo
comment
date

For the Fruits table typeid relates to other tables 1=pear, 2=apples, 3=oranges, 4=banana.
Itemid correlates to the id of either pears, apples, oranges or bananas.

Fruit 
-----------------------------------------------------------------------------
fruitsid
typeid
itemid

How would I go about selecting the top 10 from the fruit table and dynamically get the matching row from the corresponding table

Working Code

SELECT TOP (10) fruitId, id, user, photo, comment FROM 
(
    SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
    FROM FRUITS T1 LEFT JOIN PEARS T2 ON (T1.itemId=T2.id)
    WHERE T1.typeId=1
UNION ALL
    SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
    FROM FRUITS T1 LEFT JOIN APPLES T2 ON (T1.itemId=T2.id)
    WHERE T1.typeId=2
UNION ALL
    SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
    FROM FRUITS T1 LEFT JOIN ORANGES T2 ON (T1.itemId=T2.id)
    WHERE T1.typeId=3
UNION ALL 
    SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
    FROM FRUITS T1 LEFT JOIN BANANAS T2 ON (T1.itemId=T2.id)
    WHERE T1.typeId=4
)a
ORDER BY fruitId DESC

OLD NON-Working Code:

SELECT TOP (10) id, user, photo, comment FROM
(

SELECT f.typeid, f.itemid, a.id, a.user, a.photo, a.comment FROM Fruit f INNER JOIN a on f.item=a.id

--excuse the gibberish. I never used CASE before
case when f.typeid=1 then
(SELECT id, user, photo, comment FROM Pears WHERE id=f.itemid)a
case when f.typeid=2 then
(SELECT id, user, photo, comment FROM Apples WHERE id=f.itemid)a
case when f.typeid=3 then
(SELECT id, user, photo, comment FROM Oranges WHERE id=f.itemid)a
case when f.typeid=4 then
(SELECT id, user, photo, comment FROM Bananas WHERE id=f.itemid)a
)

Something like that, but I dont know how to express it correctly. Expected results below by selecting top (3) from following ‘fruit’ table order by fruitsid DESC.

fruits
-----------
fruitsid   typeid     itemid
22         1          19
23         3          73
24         2          46

pears
--------------
id     user     photo    comment    date
19     tom      1.jpg    hi         2012-06-01 12:00:00.000
22     bill     5.jpg    hello      2012-06-01 13:00:00.000

apples
--------------
id     user     photo    comment
46     sam      78.jpg   howdy
22     bill     5.jpg    hello

bananas
--------------
id     user     photo    comment
32     tom      1.jpg    hi
73     bill     5.jpg    hello

oranges
--------------
id     user     photo    comment
73     jane     55.jpg   wave
22     bill     5.jpg    hello

results
-------------
19     tom      1.jpg    hi 
73     bill     5.jpg    hello
46     sam      78.jpg   howdy

IO Stats on 4 rows using UNION ALL with newer code

(4 row(s) affected)
Table 'pears'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'fruit'. Scan count 3, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'apples'. Scan count 1, logical reads 16, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'oranges'. Scan count 1, logical reads 115, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

At least there wasn’t one physical read which is awesome

  • 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-06T05:39:11+00:00Added an answer on June 6, 2026 at 5:39 am

    One way of doing it is by using UNION to join each –

    SELECT TOP (10) fruitId, id, user, photo, comment FROM 
    (
        SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
        FROM FRUITS T1 LEFT JOIN PEARS T2 ON (T1.itemId=T2.id)
        WHERE T1.typeId=1
    UNION ALL
        SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
        FROM FRUITS T1 LEFT JOIN APPLES T2 ON (T1.itemId=T2.id)
        WHERE T1.typeId=2
    UNION ALL
        SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
        FROM FRUITS T1 LEFT JOIN ORANGES T2 ON (T1.itemId=T2.id)
        WHERE T1.typeId=3
    UNION ALL
        SELECT T1.fruitsId, T2.id, T2.user, T2.photo, T2.comment 
        FROM FRUITS T1 LEFT JOIN BANANAS T2 ON (T1.itemId=T2.id)
        WHERE T1.typeId=4
    )
    ORDER BY fruitId DESC
    

    If you dont want to include records which exists only in Fruits but not in PEAR|APPLES|ORACNGES|BANANAS then use INNER JOIN instead

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

Sidebar

Related Questions

HI all, i need to create an dynamic array, every time i add object,it
I need to create dynamic radio button in my table.I have a table in
We have a requirement, in which we need to create dynamic word document and
I need to provide a dynamic popup menu for all views. I can create
I need to create a dynamic array in Java, but the values type differ
I have a dynamic SQL statement I've created in a stored procedure. I need
I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I have two tables A and B, with dynamic columns where I have no
i want to write head tag. i need create dynamic <head id=htmlHead runat=server> </head>
I need to create dynamic 'Pay Now' buttons on my site, and PayPal says

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.