Querying one-to-many tables

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Querying one-to-many tables

    Hi,
    I have a database with, for the sake of simplicity, two tables with a
    one-to-many relationship. The first table is the user info and the second is
    the users' expertise.

    So, again for the sake of simplicity, the first table has the following data:
    John Doe
    Jane Doe

    And the second table would have the following data:
    John Doe, A topic
    John Doe, B topic
    Jane Doe, B topic
    Jane Doe, C topic
    Jane Doe, D topic

    I am setting up a web site where the web user would be able to search for
    people with expertise in certain topics. The problem I am having is when you
    want to search for people with expertise in both the B and the C topic. I set
    up my query so that I have an inner join between the two tables on the name but
    I can't do topic = B AND topic=C or I get no results, obviously. But I also
    can't do OR because then I get both Jane and John.

    I realize that this is more an SQL question than Coldfusion and that there is
    likely a very obvious answer that I am missing, but this is driving me crazy.
    How do I write this query so that I get only people that have expertise in both
    (or at least) B and C?

    Any help is greatly appreciated,
    v_roma

    v_roma Guest

  2. Similar Questions and Discussions

    1. Querying related tables
      Simple query question - I have a real estate table with various fields (Beds, Baths, SqFt, etc). One of the fields is BrokerID, which is foreign...
    2. Querying data that matches in two different tables
      The code is attached. Basically there are two tables, contact and recruiter. When you initially add someone to contact, it also adds certain...
    3. Querying for unmatched records in two tables
      I have two tables - One contains a list of all the zip codes in the US. The other table is basically a mailing list containing addresses of people...
    4. Querying a Db Using a List Box
      Hello Everyone, My Boss and I are stuck. This seems like a rather simple thing to do, but cannot get it to work. Basically, we have a list box of...
    5. querying a view of a lot tables
      I have setup a database to record our IP Account from ALL of our routers in a form. It's very slow and sometimes times out.. which is NO GOOD.. I...
  3. #2

    Default Re: Querying one-to-many tables

    v_roma,

    Assuming your second table ("userTopics") does not allow duplicates, you
    could use something like this:

    SELECT u.UserName, COUNT(*) AS TopicNameCount
    FROM user u INNER JOIN userTopics ut ON u.userName = ut.userName
    WHERE ut.TopicName IN ('B topic', 'C topic')
    GROUP BY u.UserName
    HAVING COUNT(*) = 2

    * Note - 2 is the number of topics you're matching (ie. B Topic AND C Topic)

    Disclaimer: Obviously, this is a horrible query since it contains potential
    reserved words (ie. User) and you wouldn't want to store the text value
    "userName" as the FK in another table ;-)




    mxstu Guest

  4. #3

    Default Re: Querying one-to-many tables

    Thanks, mxstu, that worked great. I'm not sure what you meant by the second
    table not allowing duplicates. There won't be any duplicate user/topic
    combinations but there will be "duplicate" topics for different users. Either
    way, your suggestion worked.

    v_roma Guest

  5. #4

    Default Re: Querying one-to-many tables

    >There won't be any duplicate user/topic combinations but there will be
    >"duplicate" topics for different users.
    Yes. That's exactly what I mean. No duplicate user/topic combinations.
    mxstu Guest

  6. #5

    Default Re: Querying one-to-many tables

    I'd use three tables for this - users, topics and users_topics. user_topics
    would have a many to many relationship to both other tables. Then I could do
    this (syntax is db specific of course)

    select thefields
    from users u join users_topics ut using (user_id)
    join topics t using (topic_id)
    where topic_id in ('B','C')




    Originally posted by: v_roma
    Hi,
    I have a database with, for the sake of simplicity, two tables with a
    one-to-many relationship. The first table is the user info and the second is
    the users' expertise.

    So, again for the sake of simplicity, the first table has the following data:
    John Doe
    Jane Doe

    And the second table would have the following data:
    John Doe, A topic
    John Doe, B topic
    Jane Doe, B topic
    Jane Doe, C topic
    Jane Doe, D topic

    I am setting up a web site where the web user would be able to search for
    people with expertise in certain topics. The problem I am having is when you
    want to search for people with expertise in both the B and the C topic. I set
    up my query so that I have an inner join between the two tables on the name but
    I can't do topic = B AND topic=C or I get no results, obviously. But I also
    can't do OR because then I get both Jane and John.

    I realize that this is more an SQL question than Coldfusion and that there is
    likely a very obvious answer that I am missing, but this is driving me crazy.
    How do I write this query so that I get only people that have expertise in both
    (or at least) B and C?

    Any help is greatly appreciated,
    v_roma



    Dan Bracuk Guest

  7. #6

    Default Re: Querying one-to-many tables

    Originally posted by: Dan Bracuk
    I'd use three tables for this - users, topics and users_topics. .....

    Based on the OP's comment "... again for the sake of simplicity, the first
    table has the following data ...., I would assume that is not their real (or
    complete) table structure.



    mxstu Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139