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

  • Home
  • SEARCH
  • 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 49119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:23:12+00:00 2026-05-10T16:23:12+00:00

The problem itself is simple, but I can’t figure out a solution that does

  • 0

The problem itself is simple, but I can’t figure out a solution that does it in one query, and here’s my ‘abstraction’ of the problem to allow for a simpler explanation:

I will let my original explenation stand, but here’s a set of sample data and the result i expect:

Ok, so here’s some sample data, i separated pairs by a blank line

------------- | Key |  Col | (Together they from a Unique Pair) -------------- |  1     Foo | |  1     Bar | |            | |  2     Foo | |            | |  3     Bar | |            | |  4     Foo | |  4     Bar | -------------- 

And the result I would expect, after running the query once, it need to be able to select this result set in one query:

1 - Foo 2 - Foo 3 - Bar 4 - Foo 

Original explenation:

I have a table, call it TABLE where I have a two columns say ID and NAME which together form the primary key of the table. Now I want to select something where ID=1 and then first checks if it can find a row where NAME has the value ‘John’, if ‘John’ does not exist it should look for a row where NAME is ‘Bruce’ – but only return ‘John’ if both ‘Bruce’ and ‘John’ exists or only ‘John’ exists of course.

Also note that it should be able to return several rows per query that match the above criteria but with different ID/Name-combinations of course, and that the above explanation is just a simplification of the real problem.

I could be completely blinded by my own code and line of thought but I just can’t figure this out.

  • 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. 2026-05-10T16:23:13+00:00Added an answer on May 10, 2026 at 4:23 pm

    This is fairly similar to what you wrote, but should be fairly speedy as NOT EXISTS is more efficient, in this case, than NOT IN…

    mysql> select * from foo; +----+-----+ | id | col | +----+-----+ |  1 | Bar |  |  1 | Foo |  |  2 | Foo |  |  3 | Bar |  |  4 | Bar |  |  4 | Foo |  +----+-----+  SELECT id      , col   FROM foo f1   WHERE col = 'Foo'    OR ( col = 'Bar' AND NOT EXISTS( SELECT *                                       FROM foo f2                                     WHERE f1.id  = f2.id                                        AND f2.col = 'Foo'                                   )       );   +----+-----+ | id | col | +----+-----+ |  1 | Foo |  |  2 | Foo |  |  3 | Bar |  |  4 | Foo |  +----+-----+ 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 108k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, that is not possible. If you are looking into… May 11, 2026 at 9:10 pm
  • Editorial Team
    Editorial Team added an answer Data-binding to lists follows the following strategy: does the data-source… May 11, 2026 at 9:10 pm
  • Editorial Team
    Editorial Team added an answer Java does not have pass by reference (which you were… May 11, 2026 at 9:10 pm

Related Questions

The problem itself is simple, but I can't figure out a solution that does
I am using Python to read in data in a user-unfriendly format and transform
Let G be a graph. So G is a set of nodes and set
I have created a simple JSF image browsing app, and I'm having a problem
I was asking a related question but messed the title up and no-one would

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.