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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:17:45+00:00 2026-06-02T23:17:45+00:00

I have a table like this Table —– userid fieldid fieldvalue where userid and

  • 0

I have a table like this

Table
-----
userid
fieldid
fieldvalue

where userid and fieldid are the primary key pair for this table.

I want to make an sql query that finds all users that have fieldvalue equal to something for a selected fieldid

For example, for the values

fieldid: 817
fieldvalue: 'yes'

I can have an sql query like:

select userid FROM table where (fieldid=817 AND fieldvalue='yes') 

This query works fine.

However if i have a second or a third criterion, making the query
like this:

select userid 
FROM table 
where (fieldid=817 AND fieldvalue='yes')
AND (fieldid=818 AND fieldvalue='no') 

returns an empty result but the conditions are satisfied in the
individual criterion.

Is there any way to correct this ?

update

i forgot to write down a use case (appologies)

userid, fieldid, fieldvalue
1 , 817, yes
1, 818, no
1, 825, yes 
2, 817, yes 
2, 818, yes
3, 829, no

for this table i want an sql query that finds the users that have the following conditions satisfied : The fieldid 817 has a value of yes and the fieldid 818 a value of no

Using the OR suggestions i had so far satisfied either the fieldid 817 to have a value of yes OR the fieldid 818 to have a value of no

I want both conditions to be satisfied. In the above the expected result will be

userid
1

and not

userid
1
2 

Since userid 2 doesn’t satisfy both conditions. Apologies for the confusion.

  • 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-02T23:17:47+00:00Added an answer on June 2, 2026 at 11:17 pm

    You should use an OR between your different criteria.

    SELECT userid 
    FROM table 
    WHERE (fieldid=817 AND fieldvalue='yes')
        OR (fieldid=818 AND fieldvalue='no')
    

    The difference between using AND/OR

    The AND operator displays a record if both the first condition and the
    second condition is true.

    The OR operator displays a record if either the first condition or the
    second condition is true.

    EDIT: Based on your comments you can do it the following way

    select t1.userid 
    FROM temp t1
    where (t1.fieldid=817 AND t1.fieldvalue='yes') 
        AND EXISTS (SELECT userid 
                FROM temp t 
                WHERE t.userid =  t1.userid 
                    AND fieldid=818 
                    AND fieldvalue='no') 
    

    see a sqlfiddle with a working copy

    or even this way

    select t1.userid 
    FROM temp t1
    left join temp t2
        on t1.userid = t2.userid
    where (t1.fieldid=817 AND t1.fieldvalue='yes')
        AND t2.fieldid=818 AND t2.fieldvalue='no'
    

    here is another sqlfiddle

    if you had more fields that you wanted to join on, then you would do the following:

    select t1.userid 
    FROM temp t1
    left join temp t2
        on t1.userid = t2.userid
    left join temp t3
        on t1.userid = t3.userid
    where (t1.fieldid=817 AND t1.fieldvalue='yes')
        AND (t2.fieldid=818 AND t2.fieldvalue='no')
        AND (t3.fieldid=819 AND t3.fieldvalue='no')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- |
Let's say I have table like this: id: PRIMARY INT price: INT (in dollars)
I have a table like this C1 C2 1 0 1 1 i want
I have a table like this ... Key Seq Val A 1 123 A
I have problem with sql query. For example, I have table like this: ID
I have a table like this: id make model year 1 chevy camaro 1969
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have a table like this: name code group john 12 smith 15 how
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName

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.