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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:47:43+00:00 2026-05-16T03:47:43+00:00

Say I have four tables, users , contacts , files , and userfiles .

  • 0

Say I have four tables, users, contacts, files, and userfiles.

Users can upload files and have contacts. They can choose to share their uploaded files with their contacts.

When a user selects one or more of their uploaded files, I want to show a list of their contacts that they are not already sharing all of their selected files with. So if they selected one file, it’d show the contacts that can’t already see that file. If the selected multiple files, it’d show the contacts that can’t already see all of the files.

Right now I’m trying a query like this (using sqlite3):

select users.user_id, users.display_name
from users, contacts, userfiles
where contacts.user_id = :user_id
and contacts.contact_id = users.user_id
and (
    userfiles.user_id != users.user_id
    and userfiles.file_id != :file_id
);

Note that the last line is auto-generated in a loop in the case of multiple selected files.

Where :user_id is the user trying to share the file, and :file_id is the file which, if a user can already see that file, they are omitted from the result. What I end up with is a list of contacts which are sharing any files other than the selected one, so if the user is sharing multiple files with any one contact, that contact shows up in the list multiple times.

How can I avoid the duplicates? I just want to check if the file is already being shared, not grab all of the contents of userfiles that don’t involve a particular file or files.

  • 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-16T03:47:44+00:00Added an answer on May 16, 2026 at 3:47 am

    select users.user_id, users.display_name
    from users, contacts as c
    where c.user_id = :user_id
    and c.contact_id = users.user_id
    and not exists (
    select user_id
    from userfiles as uf
    where uf.user_id = c.contact_id
    and uf.file_id in (:file_ids)
    );

    Note that :file_ids is all your file_id’s, seperated with commas. No more looping to run multiple queries!

    EDIT:

    This is the data I’m running as a test:

    create table users (user_id integer primary key, display_name text);
    insert into users values (1,"bob");
    insert into users values (2,"jim");
    insert into users values (3,"bill");
    insert into users values (4,"martin");
    insert into users values (5,"carson");
    
    create table contacts values (user_id integer, contact_id integer);
    insert into contacts select u1.user_id, u2.user_id from users u1, users u2 where ui.user_id!=u2.user_id;
    
    create table userfiles (user_id integer, file_id integer);
    insert into userfiles values (1,10);
    insert into userfiles values (2,10);
    insert into userfiles values (3,10);
    insert into userfiles values (4,10);
    insert into userfiles values (1,20);
    insert into userfiles values (2,30);
    

    Then, if I run my query with :user_id = 5 and :files_id = 20,30, I get:

    select users.user_id, users.display_name
    from users, contacts as c
    where c.user_id = 5
    and c.contact_id = users.user_id
    and not exists (
        select user_id
        from userfiles as uf
        where uf.user_id = c.contact_id
        and uf.file_id in (20,30)
    );
    
    UserID|Display_Name
    3     |bill
    4     |martin
    

    That seems like what you want, as I understand it, that is, the only users who do not have all the file ID’s. If I misunderstood something, please let me know.

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

Sidebar

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.