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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:44:59+00:00 2026-05-13T05:44:59+00:00

I am putting together a nice little database for adding values to options, all

  • 0

I am putting together a nice little database for adding values to options, all these are setup through a map (Has and Belongs to Many) table, because many options are pointing to a single value.

So I am trying to specify 3 option.ids and a single id in a value table – four integers to point to a single value. Three tables. And I am running into a problem with the WHERE part of the statement, because if multiple values share an option there are many results. And I need just a single result.

SELECT value.id, value.name FROM value
 LEFT JOIN (option_map_value, option_table)
 ON (value.id = option_map_value.value_id AND option_map_value.option_table_id = option_table.id)
 WHERE option_table.id IN (5, 2, 3) AND value.y_axis_id = 16;

The problem with the statement seems to be the IN on the WHERE clause. If one of the numbers are different in the IN() part, then there are multiple results – which is not good.

I have tried DISTINCT, which again works if there is one result, but returns many if there is many. The closest we have gotten to is adding a count – to return to value with the most options at the top.

So is there a way to do the WHERE to be more specific. I cannot break it out into option_table.id = 5 AND option_table.id = 2 – because that one fails. But can the WHERE clause be more specifc?

Maybe it is me being pedantic, but I would like to be able to return just the single result, instead of a count of results… Any ideas?

  • 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-05-13T05:44:59+00:00Added an answer on May 13, 2026 at 5:44 am

    The problem with the statement seems to be the IN on the WHERE clause. If one of the numbers are different in the IN() part, then there are multiple results – which is not good. I have tried DISTINCT, which again works if there is one result, but returns many if there is many. The closest we have gotten to is adding a count – to return to value with the most options at the top.

    You were very close, considering the DISTINCT:

        SELECT v.id, 
               v.name 
          FROM VALUE v
     LEFT JOIN OPTION_MAP_VALUE omv ON omv.value_id = v.id
     LEFT JOIN OPTION_TABLE ot ON ot.id = omv.option_table_id
         WHERE ot.id IN (5, 2, 3) 
           AND v.y_axis_id = 16
      GROUP BY v.id, v.name
        HAVING COUNT(*) = 3
    

    You were on the right track, but needed to use GROUP BY instead in order to be able to use the HAVING clause to count the DISTINCT list of values.

    Caveat emptor:
    The GROUP BY/HAVING COUNT version of the query is dependent on your data model having a composite key, unique or primary, defined for the two columns involved (value_id and option_table_id). If this is not in place, the database will not stop duplicates being added. If duplicate rows are possible in the data, this version can return false positives because a value_id could have 3 associations to the option_table_id 5 – which would satisfy the HAVING COUNT(*) = 3.

    Using JOINs:

    A safer, though more involved, approach is to join onto the table that can have multiple options, as often as you have criteria:

      SELECT v.id, 
             v.name 
        FROM VALUE v
        JOIN OPTION_MAP_VALUE omv ON omv.value_id = v.id
        JOIN OPTION_TABLE ot5 ON ot5.id = omv.option_table_id
                             AND ot5.id = 5
        JOIN OPTION_TABLE ot2 ON ot2.id = omv.option_table_id
                             AND ot2.id = 2
        JOIN OPTION_TABLE ot3 ON ot3.id = omv.option_table_id
                             AND ot3.id = 3
       WHERE v.y_axis_id = 16
    GROUP BY v.id, v.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer An O(M log N) solution Let the length of arr1… May 15, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer (Similar to this question: Difference between null==object and object==null) I… May 15, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer You can do this: $('.textbox1, .textbox2, .textbox3').each(function() { foo(this); });… May 15, 2026 at 6:20 am

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.