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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:38:25+00:00 2026-05-15T00:38:25+00:00

I thought that the query below would naturally do what I explain, but apparently

  • 0

I thought that the query below would naturally do what I explain, but apparently not…

My table looks like this:

id | name | g | partner | g2
1 | John | M | Sam | M
2 | Devon | M | Mike | M
3 | Kurt | M | Susan | F
4 | Stacy | F | Bob | M
5 | Rosa | F | Rita | F

I’m trying to get the id where either the g or g2 value equals ‘M’… But, a record where both the g and g2 values are ‘M’ should return two lines, not 1.

So, in the above sample data, I’m trying to return:

$q = pg_query("SELECT id FROM mytable WHERE ( g = 'M' OR g2 = 'M' )");

1
1
2
2
3
4

But, it always returns:

1
2
3
4
  • 1 1 Answer
  • 1 View
  • 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-15T00:38:25+00:00Added an answer on May 15, 2026 at 12:38 am

    Your query doesn’t work because each row is returned only once whether it matches one or both of the conditions. To get what you want use two queries and use UNION ALL to combine the results:

    SELECT id FROM mytable WHERE g = 'M'
    UNION ALL
    SELECT id FROM mytable WHERE g2 = 'M'
    ORDER BY id
    

    Result:

    1
    1
    2
    2
    3
    4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always thought that you could use OR in a LIKE statment to query
I always thought that LIMIT in a query selects only between numbers I set.
I thought that by default my Regex would exhibit the greedy behavior that I
I thought that calling BeginInvoke more than once on the same delegate instance would
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
I thought that layout is just a widget that keeps more widgets inside. But
I thought that in principle Haskell's type system would forbid calls to impure functions
I've been trying to create a conditional where clause for my query below but
I would like to get to the bottom of this because it's confusing me.
I'm making a query that pulls a lot of data from a sql table.

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.