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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:27:34+00:00 2026-06-14T12:27:34+00:00

I am looking for the most efficient way to create the following functionality. A

  • 0

I am looking for the most efficient way to create the following functionality. A user will load a page, which will give them a random post (from the user_post table), but must exclude posts that they have previously viewed. My data structure is (simplified):

user_post
---------
id (PK)


user_post_view
--------------
id (PK)
user_id
user_post_id

Where user_post is a stored post, and user_post_view is relates a user to a post that they have viewed. So, in English the query would be:

Select a random post from the user_post table that there is no user_post_view record for

I am using the Yii framework, but even a “normal” SQL statement will help.

  • 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-14T12:27:35+00:00Added an answer on June 14, 2026 at 12:27 pm
    SELECT * FROM user_post p where 
    your_user_id NOT IN (SELECT user_id FROM user_post_view WHERE user_post_id = p.id)
    

    EDIT:

    EXISTS actually probably makes more sense here…and I tacked on your LIMIT 1 in case you just wanted 1 post

    SELECT * FROM user_post p where 
    NOT EXISTS (SELECT user_id FROM user_post_view WHERE user_post_id = p.id AND 
    user_id = your_user_id) LIMIT 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to find the most efficient way to create an application that
I am looking for a most efficient way to re-use widgets with different selectors...
I am looking for the most efficient way to update a number of rows
I have a bipartite graph and I'm looking for the most efficient iterative way
I'm looking for some general advice on the most efficient way to go about
I'm looking for the most efficient way to rename (append-1, -2 etc.) a variable,
I am looking for the most efficient way to copy the content of a
What is the most efficient way to create a constant (never changes at runtime)
I am looking for some direction on how the most efficient way to structure
I'm looking for the most efficient way to remove users with duplicate emails in

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.