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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:18:40+00:00 2026-06-01T01:18:40+00:00

To simplify the description, I’ll use a member table and a mail table. The

  • 0

To simplify the description, I’ll use a member table and a mail table. The mail table has the following structure:

-------------------------------
FromMember  int  NOT NULL (FK)
ToMember    int  NOT NULL (FK)
...
-------------------------------

I want to write a query which will return all the mail for a particular member whether he is the FromMember or the ToMember. That’s easy just use an OR clause in the WHERE clause.

But where it gets complicated is that I want to join the mail table to the member table to retrieve the member info for the other member. So if I’m the FromMember than the join clause to the member table would use the ToMember field and if I’m the ToMember than the join clause would use the FromMember field. Basically, the ON clause needs to join with two different fields based on a specific condition. Is that possible?

My solution so far is to use UNION ALL as follows:

SELECT * FROM mail INNER JOIN member ON mail.ToMember = member.MemberID
WHERE mail.FromMember = @me
UNION ALL
SELECT * FROM mail INNER JOIN member ON mail.FromMember = member.MemberID
WHERE mail.ToMember = @me

However, I would like to put this query in a view because the resultset will be used in many places in my code. That means that I would need a parametrized view which I think I need to implement using a User-Defined Table Function.

Basically, I’m looking for two answers:
Is there a better way to write that query instead of UNION ALL?
And should I put that query in a VIEW or a UDF?

If VIEW is the preferred method, how can I pass the @me parameter to the VIEW. I cannot move the @me param to the WHERE clause has some people have said in their answers because the WHERE clause depends on the ON clause. Pulling out the WHERE clause and then ORing them will not return the same resultset as the above UNION ALL query.

  • 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-01T01:18:41+00:00Added an answer on June 1, 2026 at 1:18 am

    1.

    SELECT * 
     FROM mail 
      INNER JOIN member 
       ON (mail.FromMember = @me and mail.ToMember = member.MemberID)
        or (mail.ToMember = @me and mail.FromMember = member.MemberID)
    
    1. Usually views are faster then UDFs.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

to simplify this let take that table: table1 ------------- id unique primary int myVal1
How can I simplify multiple 'not in' queries? Is it efficient to use multiple
I'll simplify the problem as much as possible: I have an oracle table: row_priority,
I have the following two tables activity(activity_id, title, description, group_id) statistic(statistic_id, activity_id, date, user_id,
Simplify the following xPath so that it is as small as possible but still
To simplify this posed question, assume that each cell has a Row Name and
#!/usr/bin/python # # Description: I try to simplify the implementation of the thing below.
I'm having a rather silly problem. I'll simplify the situation: I have a table
To simplify the problem down for the question, I have a listings table and
EDIT How to simplify the following code: if(x(a) > x(b)) s = b; e

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.