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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:03:37+00:00 2026-05-13T16:03:37+00:00

I have 2 tables: ATTACHEMENTS and EMAILS (analogic example). ATTACHEMENTS table holds for each

  • 0

I have 2 tables: ATTACHEMENTS and EMAILS (analogic example). ATTACHEMENTS table holds for each row ReferenceID to email it belongs to. Also I email can have 0 or more attachements.

Tables looks like:

Emails
    UID Column1, Column2 ...up to 20 columns
    1       
    2       
    3       
    4       

Attachements
    UID     ReferenceID //link to EMAILS.UID
    1       2 //this and the one below belong to the same parent.
    2       2
    3       1
    4       3

Now I would need to retrieve all emails with attachement, no matter how many they have.
I need to get all columns so I guess I cannot use distinct, but I need only rows with differente ReferenceID. From the table above, I would need to have result of 3.
In the short: How to select rows with all columns but with distinct referenceID?
Thank you

  • 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-13T16:03:38+00:00Added an answer on May 13, 2026 at 4:03 pm

    UPDATE: I removed my previous answer because it is no longer relevant

    Ah okay, this should do it. Note that I’m using Microsoft SQL Server table variables for this complete example, just change the select statement to use “Emails” & “Attachements” instead of “@Emails” & “@Attachements” for your tables.

    DECLARE @Emails TABLE
    (
        [UID] INT,
        [Column1] VARCHAR(20),
        [Column2] VARCHAR(20),
        [Column3] VARCHAR(20),
        [Column4] VARCHAR(20),
        [Column5] VARCHAR(20)
    )
    
    DECLARE @Attachements TABLE
    (
        [UID] INT,
        [ReferenceID] INT
    )
    
    INSERT INTO @Emails ([UID], Column1, Column2, Column3, Column4, Column5) SELECT 1, 'Data 1', 'Data 2', 'Data 3', 'Data 4', 'Data 5'
    INSERT INTO @Emails ([UID], Column1, Column2, Column3, Column4, Column5) SELECT 2, 'Data 6', 'Data 7', 'Data 8', 'Data 9', 'Data 10'
    INSERT INTO @Emails ([UID], Column1, Column2, Column3, Column4, Column5) SELECT 3, 'Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'
    INSERT INTO @Emails ([UID], Column1, Column2, Column3, Column4, Column5) SELECT 4, 'Data 16', 'Data 17', 'Data 18', 'Data 19', 'Data 20'
    
    INSERT INTO @Attachements ([UID], [ReferenceID]) SELECT 1, 2
    INSERT INTO @Attachements ([UID], [ReferenceID]) SELECT 2, 2
    INSERT INTO @Attachements ([UID], [ReferenceID]) SELECT 3, 1
    INSERT INTO @Attachements ([UID], [ReferenceID]) SELECT 4, 3
    
    -- And here's the select!
    SELECT e.UID, e.Column1, e.Column2, e.Column3, e.Column4, e.Column5
    FROM @Emails e
    WHERE EXISTS
        (SELECT 1 FROM @Attachements a WHERE a.ReferenceID = e.UID)
    

    You could also do a DISTINCT with an inner JOIN, though I prefer the style above personally. I’m not sure which is more efficient.

    SELECT DISTINCT e.UID, e.Column1, e.Column2, e.Column3, e.Column4, e.Column5
    FROM @Emails e
        INNER JOIN @Attachements a ON a.ReferenceID = e.UID
    

    P.S. If the table names are in English, it’s “Attachments” not “Attachements”. If it’s in another language, then ignore me! 🙂

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

Sidebar

Related Questions

I have a table called informations (http://pastebin.com/h3UiUhpx) and also have this associated tables educations,
I have tables linked by FK, I query on the first table using entity
I have two tables as defined below: EMAILS ID SUBJECT BODY 1 Test1 blah
I have these related tables in my database: table [Files]: FileID FileName ------ --------
I have a website where a user can send out emails from. I have
I have at least 100 xml files each about 300 MB with email messages
I have three tables: Post, Attachment, and Media. Posts have Attachments, and Attachments have
I have a table which has the below columns. Ticket_id (Primary key, Int) Attachment1
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables named news and tags_news. Given a news id I have to

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.