Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I (SELECT) populate two objects from two tables with a single query?
    primarykey
    data
    text
    <p>This should be something encountered by programmers often, but I never tried to get things this way.</p> <p>That is, I'll explain. Say, I need to fetch values from table Zoo like this: </p> <pre><code> @"SELECT z.predator, z.prey FROM Zoo AS z WHERE z.preyType=@carnivore" </code></pre> <p>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:</p> <pre><code> foreach (Zoo z in lstZoo) { Animal predator = GetFromAnimalTable(z.Predator) Animal prey = GetFromAnimalTable(z.Prey) } </code></pre> <p>This can make the program slower. Can I query all the details in one go? Something like this:</p> <pre><code> SELECT (SELECT * FROM ANIMAL WHERE id=z.predator), (SELECT * FROM ANIMAL WHERE id=z.prey) FROM Zoo AS z WHERE z.preyType=@carnivore </code></pre> <p>Provided I can read the values to a new bigger object? Or is this considered a bad practice?</p> <p><strong>UPDATE:</strong> Is it a standard practice to do this? Or is it recommended to individually populate as I stated first?</p> <p><strong>UPDATE 2</strong>: 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.</p> <p>Here is how my actual query would be: </p> <pre><code>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 </code></pre> <p>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.</p> <p>A pseudo code would look like this: </p> <pre><code>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 </code></pre> <p>It should be pretty easy to extend my requirement from the answers here, but no success till now. I tried:</p> <pre><code>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 </code></pre> <p>And many variants of this with and without brackets. How can the above query be properly structured?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload