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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:00:59+00:00 2026-06-01T14:00:59+00:00

There might be an easy solution here, but it seems to have me tripped

  • 0

There might be an easy solution here, but it seems to have me tripped up. I’m trying to query a table based on an array of values in two columns. Here is the pertinent table structure and sample data

comment table
id, userId, articleId .... etc etc

article table
id, userId .... etc etc

Data:  UserIds = 3, 10.   ArticleIds = 1, 2

Let’s say I’m trying to find all the comments for a particular set of article IDs: 1,2

I can easily use this query

select * from comments WHERE articleId IN(1,2)

However, here is where it gets complex. I have a query that executes prior to the comments query that determines the appropriate article IDs. Those IDs are in an array. Also in an array are the corresponding user IDs for each article.

What I want to do now is query the comments table for only the articles in the array (1,2) AND only for those comments made by the original author (3, 10).

The simple query above will bring back all the comments for articleId 1 and 20. So for example I can’t figure out where to add another conditional that says onyl comments for articleId 1, 20 AND corresponding userId, 3, 10.

Any help or suggestions would be much appreciated!

  • 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-01T14:01:00+00:00Added an answer on June 1, 2026 at 2:01 pm

    I think the simplest way is to write:

    SELECT comments.*
      FROM articles
      JOIN comments
        ON articles.id = comments.articleId
       AND articles.userId = comments.userId
     WHERE articles.id IN (1, 2)
    ;
    

    The AND articles.userId = comments.userId clause is what enforces your “only for those comments made by the original author” requirement.

    Alternatively, you can use an EXISTS clause:

    SELECT *
      FROM comments
     WHERE articleId IN (1, 2)
       AND EXISTS
            ( SELECT 1
                FROM articles
               WHERE id = comments.articleId
                 AND userId = comments.userId
            )
    ;
    

    or a single-row subquery:

    SELECT *
      FROM comments
     WHERE articleId IN (1, 2)
       AND userId =
            ( SELECT userId
                FROM articles
               WHERE id = comments.articleId
            )
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There might be a really simple solution to this question, but i just don't
There might be similar questions but I still have some parts that I couldn't
I'm sure there's an easy answer here and i'm just over-complicating things, but i
I'm still kindof new to jQuery, so there probably is an easy solution, but
Ok, this might be an easy one, but I can't find a solution. I'm
The question is extremely easy, but the solution might not. Let's say this is
this is might be a very basic question, but seems like it's not easy
I am hoping that there might be an easy way to do this, I
I realise there might be similar questions but I couldn't find one that was
I guess there might be some overlapping with previous SO questions, but I could

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.