Results 1 to 13 of 13

Thread: Need query help in one to many relationship

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Need query help in one to many relationship

    I have these tables

    Boxes:
    id | name
    1 | oranges
    2 | mangoes
    3 | Bananas
    4 | misc

    thingsinboxes:
    id | box_id | thing
    1 | 1 | a
    2 | 1 | b
    3 | 2 | c
    4 | 3 | d
    5 | 3 | e
    6 | 4 | a
    7 | 4 | b
    8 | 4 | c


    How do i get thingsinboxes.box_id that has thing='a' AND thing='b'

    i dont want anything returned when i just say thing ='a' because i am leaving out thing='b'

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need query help in one to many relationship

    First up, there are no records with thing = 'a' AND thing = 'b'. The correct terminology here is OR:
    SQL Code:
    1. SELECT Boxes.name, thingsinboxes.thing
    2. FROM Boxes INNER JOIN thingsinboxes
    3. ON Boxes.id = thingsinboxes.box_id
    4. WHERE thing = 'a' OR thing = 'b'
    Secondly, I'm afraid that this doesn't make sense, to me at least:
    i dont want anything returned when i just say thing ='a' because i am leaving out thing='b'
    You aren't "saying" anything. You're executing code. Vague terms like that don't really mean anything to anyone who doesn't know your project. If the user is supposed to enter data into your app then you need to explain that. If you want to hard-code values into the SQL code then you need to explain that. Provide a full and clear description each and every time to avoid confusion and time wasted by our having to ask for more information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Re: Need query help in one to many relationship

    Quote Originally Posted by jmcilhinney View Post
    SQL Code:
    1. SELECT Boxes.name, thingsinboxes.thing
    2. FROM Boxes INNER JOIN thingsinboxes
    3. ON Boxes.id = thingsinboxes.box_id
    4. WHERE thing = 'a' OR thing = 'b'
    thanks for the response. But what is the query to return 1 when i need to get a result only if thing='a' and thing='b' ? That is two conditions. i have two dropdowns and i need to get boxes.id when only both the conditions are met.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need query help in one to many relationship

    So, what you mean is that you want to get the ID where there is a thingsinboxes record for 'a' and a thingsinboxes record for 'b'

    There are various ways to do that kind of thing, including this:
    Code:
    SELECT box_id
    FROM thingsinboxes
    WHERE box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')
      AND box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'b')
    ...with the box info too:
    Code:
    SELECT id, name
    FROM boxes
    WHERE id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')
      AND id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'b')

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Re: Need query help in one to many relationship

    ok we are getting closer.

    what if i dont want anything returned for this -
    SELECT box_id
    FROM thingsinboxes
    WHERE box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')

    but i want 1 returned for this -
    SELECT box_id
    FROM thingsinboxes
    WHERE box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')
    AND box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'b')

    the problem with first query is it will return a result even though i want a result ONLY if all the box_id s are passed.

    thank you for your patience

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need query help in one to many relationship

    I don't know where that first query came from, or why you posted it.

    Have you tried running the query I posted?

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Re: Need query help in one to many relationship

    Quote Originally Posted by si_the_geek View Post
    I don't know where that first query came from, or why you posted it.

    Have you tried running the query I posted?
    yes. your query works. but that is not what i am looking for. I am looking for a result ONLY if both the conditions are passed. If only one condition is passed, i do not want a result.

    thanks

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need query help in one to many relationship

    I'm afraid I don't see any difference between your explanation and what the query should be doing.

    Can you show us the results of the query I posted, and the results you actually want to see?

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Re: Need query help in one to many relationship

    Code:
    SELECT box_id
    FROM thingsinboxes
    WHERE box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')
      AND box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'b')

    that is valid if i am passing both 'a' and 'b' . but i want to get the result ONLY when BOTH are passed.

    I want to get no result if i do just

    WHERE box_id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need query help in one to many relationship

    Why do you want to run anything unless both values are available?

    Based on what you have posted, you should only be running the query when both drop-downs have a selection.

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Need query help in one to many relationship

    Just as you showed the table data in psot #1, can you also show the expected result?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    14

    Re: Need query help in one to many relationship

    Quote Originally Posted by Pradeep1210 View Post
    Just as you showed the table data in psot #1, can you also show the expected result?
    id | name
    1 | oranges

    this is the result of query that passes both 'a' and 'b'

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Need query help in one to many relationship

    Si's query works great. All you need to do is pass an empty string appropriately, when one of the parameters is missing. (i.e. Pass the values of the combobox; if they have nothing pass nothing.)

    e.g.
    If you have both 'a' and 'b' then:
    sql Code:
    1. SELECT id, name
    2. FROM boxes
    3. WHERE id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')
    4.   AND id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'b')
    It will return appropriate result.

    Otherwise if suppose 'a' is missing then we pass empty string instead of 'a'.
    likewise, if 'b' is missing then we pass empty string instead of 'b'
    sql Code:
    1. --returns empty resultset
    2. SELECT id, name
    3. FROM boxes
    4. WHERE id IN (SELECT box_id FROM thingsinboxes WHERE thing = '')
    5.   AND id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'b')
    6.  
    7. --returns empty resultset
    8. SELECT id, name
    9. FROM boxes
    10. WHERE id IN (SELECT box_id FROM thingsinboxes WHERE thing = 'a')
    11.   AND id IN (SELECT box_id FROM thingsinboxes WHERE thing = '')
    Both of the above queries will return no rows if either 'a' or 'b' is missing.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width