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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:16:51+00:00 2026-05-28T04:16:51+00:00

This should be something encountered by programmers often, but I never tried to get

  • 0

This should be something encountered by programmers often, but I never tried to get things this way.

That is, I’ll explain. Say, I need to fetch values from table Zoo like this:

 @"SELECT z.predator, z.prey FROM Zoo AS z WHERE z.preyType=@carnivore"

Now I can get all the values to a List. I need to display the details of that querying in a grid. Now having got z.predator and z.prey values (which are for time sake integers, ie, their respective ids), I need to populate its meaningful values for displaying it to end user (I can’t just display their ids). So now I might do something like this:

 foreach (Zoo z in lstZoo)
 {
       Animal predator = GetFromAnimalTable(z.Predator)
       Animal prey = GetFromAnimalTable(z.Prey)
 }

This can make the program slower. Can I query all the details in one go? Something like this:

   SELECT (SELECT * FROM ANIMAL WHERE id=z.predator), 
          (SELECT * FROM ANIMAL WHERE id=z.prey) 
   FROM Zoo AS z WHERE z.preyType=@carnivore

Provided I can read the values to a new bigger object? Or is this considered a bad practice?

UPDATE: Is it a standard practice to do this? Or is it recommended to individually populate as I stated first?

UPDATE 2: I seem to have made a terrible mistake of not posting the query exactly as I needed. I thought I could tweak the answers from here to meet my requirement, alas no with the parenthesis construction of Access queries.

Here is how my actual query would be:

SELECT z.predator, p.lifeSpan, z.prey 
FROM Zoo AS z 
INNER JOIN Plants AS p ON p.parent_id=z.id 
WHERE z.preyType=@carnivore

Actually I had an INNER JOIN query already with another table. Now I need to get (SELECT) values of z.predator (and its corresponding values from Animals table), p.lifeSpan, z.prey (and its corresponding values from Animal table) meeting the INNER JOIN and WHERE condition.

A pseudo code would look like this:

SELECT (SELECT * FROM ANIMAL WHERE id=z.predator), p.lifeSpan, (SELECT * FROM ANIMAL WHERE id=z.prey) 
FROM Zoo AS z INNER JOIN Plants AS p ON p.parent_id=z.id 
WHERE z.preyType=@carnivore

It should be pretty easy to extend my requirement from the answers here, but no success till now. I tried:

SELECT a1.*, p.lifeSpan, a2.* 
FROM Zoo AS z 
INNER JOIN Plants AS p ON p.parent_id=z.id 
INNER JOIN Animal AS a1 ON (a1.id=z.predator)
INNER JOIN Animal AS a2 ON (a2.id=z.prey)
WHERE z.preyType=@carnivore

And many variants of this with and without brackets. How can the above query be properly structured?

  • 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-28T04:16:51+00:00Added an answer on May 28, 2026 at 4:16 am

    It seems this your latest query attempt:

    SELECT a1.*, p.lifeSpan, a2.* 
    FROM Zoo AS z 
    INNER JOIN Plants AS p ON p.parent_id=z.id 
    INNER JOIN Animal AS a1 ON (a1.id=z.predator)
    INNER JOIN Animal AS a2 ON (a2.id=z.prey)";
    WHERE z.preyType=@carnivore
    

    Discard the semicolon from inside the statement. Also discard the double quote.

    Just to simplify the SQL, exclude the WHERE clause for now.

    Then you should be in a better position to address the issue of parentheses which Access’ db engine requires for multiple joins.

    SELECT a1.*, p.lifeSpan, a2.* 
    FROM
        ((Zoo AS z 
        INNER JOIN Plants AS p ON p.parent_id=z.id) 
        INNER JOIN Animal AS a1 ON a1.id=z.predator)
        INNER JOIN Animal AS a2 ON a2.id=z.prey
    

    Notice I discarded those parentheses which enclosed the ON expressions. Simple ON expressions don’t require them. If you had a compound expression for ON, then you would need parentheses like this:

    ON (p.parent_id=z.id AND p.foo = z.bar)
    

    The sample query I suggested looks correct to me. (If it works for you, add your WHERE clause back again.) However, I don’t pay close attention to parentheses placement because I use Access’ query designer to set up joins … and it adds the parentheses the db engine requires.

    I urge you to do the same. If you’re using an Access db from Dot.Net without having a copy of Access installed, you really should get a copy. Trying to use a database without that database’s native development tools is an unreasonable challenge … somewhat like trying to type while wearing mittens.

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

Sidebar

Related Questions

Trying to figure out the best way to do this: Should I do something
This should be a elementary question but why is better to use something like
I feel like this should be a no brainer, but clearly I'm missing something...
OK, I'm feeling like this should be easy but am obviously missing something fundamental
This seems like it should be something very easy to do, but every time
It seems like this should be something built into jQuery without the need for
This seems like it should be something I already know. We need to run
It seems like there should be something shorter than this: private string LoadFromFile(string path)
I guess the procedure should be something like this: declare @db varchar(100) declare @user
Should I use something like maven for this? Or would finding them as they

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.