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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:26:49+00:00 2026-06-10T10:26:49+00:00

I have two tables Table one is a list of documents documents: +————-+—————+ |

  • 0

I have two tables

Table one is a list of documents

documents:

+-------------+---------------+
| document_ID | document_name |
+-------------+---------------+
|    1        |   test.pdf    |
+-------------+---------------+
|    2        |  other.pdf    |
+-------------+---------------+

Table two is a list of users who have used that document. It is a 1-1 relationship – i.e. a user will only ever use a document ONCE (or not at all).

documents_users:

+-------------+---------------+-----------+---------------+
| document_ID | user_ID       | has_read  | (other stuff) |
+-------------+---------------+-----------+---------------+
|    1        |   5           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    1        |   7           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    2        |   5           |     1     |      stuff    |
+-------------+---------------+-----------+---------------+

When a user is browsing the list – I need to be able to show ALL documents, plus IF they have access it or not

So for example the desired output for user 5 in the example above is;

+-------------+---------------+-----------+---------------+
| document_ID | document_name |  has_read | (other stuff) |
+-------------+---------------+-----------+---------------+
|    1        |   test.pdf    |     1     |      stuff    |
+-------------+---------------+-----------+---------------+
|    2        |  other.pdf    |     1     |      stuff    |
+-------------+---------------+-----------+---------------+

while the desired output for user 7 is

+-------------+---------------+-----------+---------------+
| document_ID | document_name |  has_read | (other stuff) |
+-------------+---------------+-----------+---------------+
|    1        |   test.pdf    |     1     |   stuff       |
+-------------+---------------+-----------+---------------+
|    2        |  other.pdf    |           |               |
+-------------+---------------+-----------+---------------+

I tried doing a join along the lines of

$this->db->join('documents_users', 'documents_users.document_ID = documents.document_ID');
$this->db->where('documents_users.user_ID', '7');
return $this->get_all();

But this only returns the records that they has_read=1

I can see the problem is the “where” command – but I’m not sure what to do?

  • 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-10T10:26:50+00:00Added an answer on June 10, 2026 at 10:26 am

    You need a (left) outer join, with the filter criterion on user_ID moved into the join condition rather than being in a WHERE clause:

    $this->db->join(
       'documents_users',
       'documents_users.document_ID = documents.document_ID
    AND documents_users.user_ID = 7',
       'left'
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables : one table it matchs and the other is teams.
I have two tables. One table contains words. Other table contains points. table words:
In SQL Server 2008 I have two tables. One table for users: id_user -
I have two tables. In the one table I have a list of dorm
Lets say I have two tables, one for transactions, and another table who's primary
I have two tables, one table has a list of videos, one has a
I have two tables, one containing a list of different options users can select
I have two tables, one an import table, the other a FK constraint on
I have two tables. One (Widgets) has a list of widgets (ID, widget_name, color,
Quick method description: I have two tables, lets name them table ONE and table

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.