Professional Web Applications Themes

[Q] Help forming query - MySQL

Let's say I have a table T1 with column C1 which has several rows contains the values between 1 - 10. Let's say I also have a table T2 with column C2 which has a few rows containing the value, 2, 3 & 5. What I want to do is select those rows in T1 which have the values 2, 3 & 5 in C1 because the values 2, 3 & 5 are found in T2:C2. I assume this isn't a difficult query (or set of queries) to construct, but I am rather new to mysql....

  1. #1

    Default [Q] Help forming query

    Let's say I have a table T1 with column C1 which has several rows
    contains the values between 1 - 10.

    Let's say I also have a table T2 with column C2 which has a few rows
    containing the value, 2, 3 & 5.

    What I want to do is select those rows in T1 which have the values 2, 3
    & 5 in C1 because the values 2, 3 & 5 are found in T2:C2.

    I assume this isn't a difficult query (or set of queries) to construct,
    but I am rather new to mysql.

    ericgorr@gmail.com Guest

  2. #2

    Default Re: [Q] Help forming query

    <ericgorr> wrote in message
    news:1140815424.075905.6350v46g2000cwv.googlegrou ps.com...
    > What I want to do is select those rows in T1 which have the values 2, 3
    > & 5 in C1 because the values 2, 3 & 5 are found in T2:C2.
    What you want is a "join". This is an elementary concept of the SQL
    language.
    Google for "sql join tutorial". Or read any book on SQL.

    Regards,
    Bill K.


    Bill Karwin Guest

  3. #3

    Default Re: Help forming query


    Bill Karwin wrote:
    > <ericgorr> wrote in message
    > news:1140815424.075905.6350v46g2000cwv.googlegrou ps.com...
    > > What I want to do is select those rows in T1 which have the values 2, 3
    > > & 5 in C1 because the values 2, 3 & 5 are found in T2:C2.
    >
    > What you want is a "join". This is an elementary concept of the SQL
    > language.
    > Google for "sql join tutorial". Or read any book on SQL.
    Ok, so the query that seems to work is:

    SELECT DISTINCT T1.* FROM T1, T2 WHERE T1.C1 = T2.C2

    Is this how you would have written it as well?

    ericgorr@gmail.com Guest

  4. #4

    Default Re: Help forming query

    <ericgorr> wrote in message
    news:1140825912.747667.121110u72g2000cwu.googlegr oups.com...
    >
    > SELECT DISTINCT T1.* FROM T1, T2 WHERE T1.C1 = T2.C2
    >
    > Is this how you would have written it as well?
    Yep, that works fine.

    I try to avoid using * in queries, because when the table structure is
    changed at some future time, I might get fields back that I didn't intend.
    I prefer to explicitly name all the columns I need.

    I use the JOIN syntax instead of putting join conditions in the WHERE
    clause. I find it's more clear when the query gets more complex. Also,
    there are types of joins that can be done only with the JOIN syntax.

    So my version would be:

    SELECT DISTINCT T1.C1
    FROM T1 INNER JOIN T2 ON T1.C1 = T2.C2

    Regards,
    Bill K.


    Bill Karwin Guest

Similar Threads

  1. PHP User Group forming in Louisville, KY
    By Steve Magruder in forum PHP Development
    Replies: 0
    Last Post: September 17th, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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