Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should be able to use a distinct operator:</p> <pre><code>SELECT DISTINCT Customers.member_id, Books.book_title FROM Customers INNER JOIN Books ON Customers.member_id = Books.member_id </code></pre> <p>If that does not work correctly you could use an inner select:</p> <pre><code>SELECT DISTINCT * FROM (SELECT Customers.member_id, Books.book_title FROM Customers INNER JOIN Books ON Customers.member_id = Books.member_id) As newTable </code></pre> <p>Also, if this is a frequently used query I would avoid a UNION because they are known to have performance problems.</p> <p><strong>In response to the edit:</strong></p> <pre><code>SELECT Customers.member_id, Customer_Info.Name, ISNULL(newTable.book_title, '') FROM Customers INNER JOIN Customer_Info LEFT JOIN (SELECT DISTINCT member_id, book_title FROM Books) newTable ON newTable.member_ID = Customers.member_id </code></pre> <p>This should return all books associated with a customer (but only one time for each title and if no books are found then it will return an empty string.) If this does not answer the question please include some additional information about the tables and an example of the result you would like and I will update.</p> <p><strong>OK, now I think I know what you are looking for:</strong></p> <p>Here is a possible query based on your original question using the tables provided. However, it will not work if the customer has two distinct e-mail addresses set; in that case, you could add a TOP(1) to ensure only one result but you won't know if it is the "right result."</p> <pre><code>SELECT Customers.member_id, Office.Office_Name, ISNULL(newTable.email, '') FROM Customers INNER JOIN Office LEFT JOIN (SELECT DISTINCT member_id, email FROM Books WHERE email IS NOT NULL AND email &lt;&gt; '') newTable ON newTable.member_ID = Customers.member_id </code></pre> <p>Here is another query based on the data you provided and the example output.</p> <pre><code>SELECT Member_Name, Email FROM thridTable WHERE Member_Name = @SomeInputParameter </code></pre> <p>I'm not sure how representative your sample data is but why would you be storing the member name in more than one table? That is a guaranteed headache in the future.</p>
 

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