Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server JOIN - displaying two joined values in one query that map to one other table?
    primarykey
    data
    text
    <p>Here is an example of the data set I am working with. I am trying to write a query that will allow me to request a row from the items table, and display the User Name from the users table in place of the user ids:</p> <pre><code>Table 1 - users table User ID | User Name -------------------- 12 | Fred 13 | Ted 14 | Ned Table 2 - items Table Item ID | Submitted User | Assigned User ---------------------------------------- 234 | 12 | 14 345 | 12 | 13 456 | 14 | 12 </code></pre> <p>This is as far as I can get, which returns one properly labled username:</p> <pre><code>SELECT users.[user name] AS [Submitted User] FROM items JOIN users ON items.[Assigned User] = users.[User ID] WHERE items.[Item ID] = '234' </code></pre> <p>The problem with this is that while ONE field works, I need to get both the submitted user AND the assigned user. I am trying to do it in one query... I have a feeling it can be done but I just don't know exactly how. If I try to assign the fields more than once I get an error about correlation. Here is an example of what I tried that gives a correlation error:</p> <pre><code>SELECT users.[user name] AS [Submitted User], users.[user name] AS [Assigned User] FROM items JOIN users ON items.[Submitted User] = users.[User ID] JOIN users ON items.[Assigned User] = users.[User ID] WHERE items.[Item ID] = '234' </code></pre> <p>This is exactly what I'm trying to do:</p> <pre><code>SELECT items[Submitted User].users.[user name] AS [Reported User], items[Assigned User].users.[user name] AS [Assigned User] WHERE items.[Item ID] = '234'&lt;/pre&gt; </code></pre> <p>This is the response I'm trying to get:</p> <pre><code>Submitted User | Assigned user Fred - Ted </code></pre>
    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