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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:31:21+00:00 2026-06-17T09:31:21+00:00

I am not able to explain my question in abstract terms. It is a

  • 0

I am not able to explain my question in abstract terms. It is a very simple question, but I need to go through this very palpable example. It’s completely made up, and therefore should be comparable to simmilar applications.

We have a bunch of tables with information about users, all the tables are what I believe is normalized, some values are only references via IDs to other tables.

I am using mySQL ( and PHP with the mysqli extension – in case that matters, which I doubt)

So here’s for example what I have:

table user_data

=====================================================
|| User_ID || Name || age || gender || location_ID ||
=====================================================
|| U000001 || Paul || 30  || m      || L00001      ||
|| U000002 || John || 20  || m      || L00001      ||
|| U000003 || Mike || 25  || m      || L00002      ||
|| U000004 || Anna || 25  || f      || L00003      ||


table user_personal_info

============================================
|| User_ID || color || food  || profession||
============================================
|| U000001 || red   || pizza || architect ||
|| U000002 || blue  || pasta || policeman ||
|| U000003 || green || steak || plumber   ||
|| U000004 || pink  || salad || teacher   ||


table locations

========================================================
|| location_ID || country || state      || city       ||
========================================================
|| L00001      || USA     || New York   || New York   ||
|| L00002      || USA     || New York   || Buffalo    ||
|| L00003      || USA     || California || Sacramento ||
|| L00004      || Canada  || Ontario    || Toronto    ||
|| L00005      || Canada  || Quebec     || Montreal   ||



table user_activities

=========================================
|| activity_ID  || user_ID || priority ||
=========================================
|| A0003        || U000001 || 5        ||
|| A0005        || U000001 || 4        ||
|| A0004        || U000002 || 2        ||
|| A0006        || U000002 || 1        ||
|| A0001        || U000003 || 3        ||
|| A0002        || U000004 || 4        ||
|| A0001        || U000004 || 1        ||
|| A0003        || U000004 || 5        ||

table activities

=================================
|| activity_ID  || description ||
=================================
|| A0001        || surfing     ||
|| A0002        || exercising  ||
|| A0003        || baseball    ||
|| A0004        || theater     ||
|| A0005        || dancing     ||
|| A0006        || reading     ||

Alrighty, you get the concept, right?

to DISPLAY each entry, I make the following mySQL statement and then loop through the resultset in PHP and so on:

SELECT * FROM user_data
JOIN user_personal_info USING (User_ID)

in order to also display what their favorite activities are, I also have to do this:

SELECT * FROM user_activities 
WHERE user_ID = (current user_id)

of course I have to translate what the activity ID stands for and what the location ID stands for with additional queries…

(By the way: Does anyone have a better suggestion for how to display all users and all fields associated with them, rather then doing two queries?)

Now I want to build a thorough search function to find very specific users.
I would know how to filter through my results using PHP, but that would require me to download the entire db first, and that probably takes very long to do, once a couple thousand users are in the DB.

I know how to find users who are male, female, or both, who like food or a color, who are from a specific location (location_ID=L00001 or so)…
I know how to assign rules about ages (=, >, <…). I know about the LIKE %?% parameter.

My question is:

How would I find all users from a certain country or a certain state?
*How do I ask mySQL to only show those users,who’s location_ID matches one from an array of location_IDs?*

How do I find all users with one AND/OR more specific activities?
How do I ask mySQL to only show those users, who’s array of activities matches at least all activities from an array (that would be the AND version)?
*How do I ask mySQL to only show those users, who’s array of activities contains at least one of the activities from an array (that would be the OR version)?*

And now the really important question is:

How do I combine those statements with my normal statements from above?
Meaning: How would I find all users from NEW YORK STATE who are into SURFING and who are MALE and who like PIZZA?
or
How would I find all users from USA who are into READING, and DANCING and who are OVER 30 and who like GREEN?
or
How would I find all users from SACRAMENTO, CA who are PLUMBERS and FEMALE?

etc. etc. the examples are obviously endless!

I am sure someone will just be able to tell me “you should research this keyword”. But because I am unable to express my question in a conceise manner, I wasn’t successful finding much information…

UPDATE:

Thanks for the answer. There were a couple of useful things I was pointed to, here is a summary of the things I didn’t know but do now:

  • utilizing JOIN more effectively
  • the IN operator
  • the GROUP BY operator combined with HAVING COUNT()
  • and SUB SELECTS

Thanks for pointing those things out to me! 🙂

  • 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-17T09:31:21+00:00Added an answer on June 17, 2026 at 9:31 am

    Well, I think one of the keywords you’re looking for is the IN operator.

    SELECT * FROM locations WHERE country IN ('USA', 'Canada', 'Denmark')
    

    would return all the rows where one of the values in the IN-clause is matched against the country field. So it’s like writing this:

    SELECT * FROM locations WHERE country = 'USA' OR country = 'Canada' OR country = 'Denmark'
    

    As for the rest of your questions:

    Does anyone have a better suggestion for how to display all users and all fields associated with them, rather then doing two queries?

    Simply join them all together, like:

    SELECT * FROM user_data
    JOIN locations ON user_data.location_ID = locations.location_ID
    JOIN user_personal_info ON user_data.User_ID = user_personal_info.User_ID
    JOIN user_activities ON user_personal_info.User_ID = user_activities.User_ID
    JOIN activities ON user_activities.activity_ID = activities.activity_ID
    

    Of course, depending on your structure you’d use LEFT JOIN or RIGHT JOIN, etc. It’s also not a good practice to simply retrieve all data by SELECT *, but to really only select the fields you need.
    Furthermore you could/should create one/more views representing the joined data you need and select from it/them.

    How would I find all users from a certain country or a certain state?

    SELECT user_data.* FROM user_data
    JOIN locations ON user_data.location_ID = locations.location_ID
    WHERE locations.country = 'USA' AND state = 'New York'
    

    Depending on how you get the data from the user and how you prepare it for your statement in PHP. For instance, assuming your users searches for a country and you get it via a post method:

    <?php
      $country = sanitize($_POST['country']);  // assuming a sanitation function for user input
      // whether by doing a sub-select
      $sql = "SELECT user_data.* FROM user_data WHERE user_data.location_ID = (SELECT locations.location_ID FROM locations WHERE locations.country LIKE '%{$country}%')";
    
      // or doing a join
      $sql = "SELECT user_data.* FROM user_data JOIN locations ON user_data.location_ID = locations.location_ID WHERE locations.country LIKE '%{$country}%'";
    ?>
    

    The same principle goes for state, of course.

    How do I find all users with one AND/OR more specific activities?

    Here you would need to join against the activities table and use the IN operator as shown above.

    How do I combine those statements with my normal statements from above?

    Taking your example How would I find all users from NEW YORK STATE who are into SURFING and who are MALE and who like PIZZA?

    SELECT user_data.* FROM user_data
     JOIN locations ON user_data.locations_ID = locations.location_ID
     JOIN user_activities = user_data.User_ID = user_activities.user_ID
     JOIN activities ON user_activities.activity_ID = user_activities.activity_ID
    WHERE locations.sate = 'New York'
      AND activities.description IN ('surfing')
      AND user_data.gender = 'm'
      AND user_personal_info.food = 'pizza'
    

    Hope this helps and gets you in the right direction.

    UPDATE

    Of course the IN-operator here could be substituted with description = 'surfing', since it’s only one value. And you’re right if you add another value like description IN ('surfing', 'reading') it would mean surfing OR reading. So if you want to get all the users who are into surfing AND reading I guess I’d do it with a sub-select:

    SELECT user_data.* FROM user_data
     WHERE user_data.User_ID IN (
       SELECT user_activities.user_ID FROM user_activities
         JOIN activities ON user_activities.activity_ID = activities.activity_ID
        WHERE activities.description IN ('surfing', 'reading')
       GROUP BY user_activities.activity_ID
         HAVING COUNT(user_activities.user_ID) = 2
     )
    

    So the sub-select means: count each user-id that appears with a ‘surfing’ or ‘reading’, and if the count is equal to 2 (meaning they match for both) retrieve the user-id.
    And the outer select simply selects the data from every user of the subset.

    Now, I didn’t test this so it may vary. And there are probably simpler ways. At least something you could do to simplify this query is to create a view as I mentioned before and select from it.

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

Sidebar

Related Questions

I am not able to put proper title for this question, but i will
This is probably my second question regarding authentication. But i am still not able
Apologies, It looks like my original question was not able to correctly explain what
I am not able to find a similar question else where on this site,
I feel stupid asking this question, but I can not find a clear answer
I am not sure I am going to be able to explain this one
i know this is an old question, asked many times. but i am not
I'm kicking my self for not being able to do this. But I've tried
Website: http://clandestinoangusto.it/ Hi, can anybody explain why WebKit browsers are not able to highlight
not able to get this, can someone help for this LINQ query? select col1,

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.