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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:44:57+00:00 2026-06-18T02:44:57+00:00

I know I’ve done this before, but haven’t done much SQL in a while.

  • 0

I know I’ve done this before, but haven’t done much SQL in a while.

I have a table called lead_detail, it has the following columns: id, lead_id, form_id, field_number, value.

I’m trying to get results and then filter them.

So, originally, I got all the lead ids that had a particular answer for a particular question.

Let’s say the first filter was that I wanted all results for an answer of “yes” to a question of “do you like chicken” (field_number of 4).

$leadIds = query(SELECT lead_id FROM lead_detail WHERE field_number = '4' AND value = 'yes' AND form_id = '1')
$leadIds = implode(', ', $leadIds);

Then, I could get the appropriate filtered data by

SELECT * FROM lead_detail WHERE form_id = '1' and lead_id IN ($leadIds)

This works perfectly.

If I want to add a SECOND filter though, I need to do two queries to the same columns in the same query. I’m thinking I need to do some kind of join with table aliases, or some sub selects, but can’t remember how to do it.

So if I needed to know who answered “yes” to a question of “do you like chicken” AND who answered “18-24” on “what is your age”, how would I get the appropriate $leadIds?

It’d be something like the combination of

SELECT lead_id FROM lead_detail WHERE field_number = '4' AND value = 'yes' AND form_id = '1'
AND
SELECT lead_id FROM lead_detail WHERE field_number = '5' AND value = '18-24' AND form_id = '1'
  • 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-18T02:44:58+00:00Added an answer on June 18, 2026 at 2:44 am

    An inner join would work for this:

    SELECT DISTINCT(ld1.lead_id)
    FROM lead_detail ld1
    INNER JOIN lead_detail ld2 ON (ld1.lead_id = ld2.lead_id)
    WHERE ld1.field_number = '4' AND ld1.value = 'yes' AND ld1.form_id = '1' 
    AND ld2.field_number = '5' AND ld2.value = '18-24' AND ld2.form_id = '1'
    

    There is possibly a neater solution however.

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

Sidebar

Related Questions

I know I have done this before but I can't seem to remember where
I know this has been asked a million times, but I haven't had much
I know that this sort of question has been asked here before, but still
I know questions like this have been asked before, but I'm not sure how
know question is not clear at a glance, i have this table: ID Start
I know most commercial games development is done in assembly or C/C++ but has
I know jquery have backwardscompatibility files. But if you link to this specific version:
I know this has been asked but I am unable to fix it For
I know about this question , but I have the source and I'm having
I know that this has been discussed thousand times but I still cannot figure

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.