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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:16:37+00:00 2026-05-20T10:16:37+00:00

Two tables: user (id, myValue, …) user_preferred (id, userid, preferredValue) fk userid -> user(id)

  • 0

Two tables:

user (id, myValue, ...)

user_preferred (id, userid, preferredValue) fk userid -> user(id)

Explanation:

user is a table of all users. user_preferred is a table of any user’s preferred values. A user can be listed multiple times in user_preferred but must have different values. Query should return all users that have a myValue that matches the preferred value of the given user. $userid is the php variable of the user passed.

The Trick:

A user could have no preference, in which case there is no entry in the user_preference table. When the above query is done, I want to return every user if the given user has no preference.

Analogy:

I’m at a bar and the bartender asks me what I want to drink. I say give me everything he has that matches my preference. The first round I say I like crappy beers. So he gives me a Fosters. Second round I say I have no preference and he gives me 12 pints ranging from Bud Light to Guinness. Instead of beers, these would be users. Get it?

Query (so far):

SELECT * FROM user WHERE
IF ((SELECT preferredValue FROM user_preferred WHERE userid = $userid) IS NULL, 
   1, 
   user.myValue ANY (SELECT preferredValue FROM user_preferred WHERE userid = $userid)
)

Additional Trick:

I don’t want to run “SELECT preferredValue FROM user.preferred where id = $userid” twice. Can I save the results from the first run-time and somehow use it in place of the second?

  • 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-20T10:16:38+00:00Added an answer on May 20, 2026 at 10:16 am

    To return a list of users:

    SELECT o.*
      FROM `user` o
     WHERE o.id IN
         ( SELECT DISTINCT m.userid
             FROM user_preferred m
            WHERE EXISTS 
                  ( SELECT 1 FROM user_preferred p
                     WHERE p.preferredValue = m.preferredValue 
                       AND p.userid <> m.userid 
                       AND p.userid = $userid )
         )
        OR ( o.id <> $userid AND NOT EXISTS
             ( SELECT 1
                 FROM user_preferred q
                WHERE q.userid = $userid
             )
           )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got two tables: users(id, column) and objects(user_id, column) . How to extract all users
Hi I have two tables User and Pictures im trying to pull the pathname
Let's say I have two tables as shown: user id plan course_limit username 10
I have used the standard user tables that ASP.net setup and I'm looking to
I have two tables: projects and user_licenses . I'd like to grab the entire
Maybe a quite easy question but I'm new in Linq to SQL. I have
I am using Hibernate in Spring MVC 3.05 and an Oracle database. I have
Hey guys bit of a complication here, I have a create account page and
Trying to use the getDependentRowset in a ZF application. I have the following relationship

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.