SQL question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay

    #1

    SQL question

    Hi, I'm an sql noob and I would like to know if there is an easy way to
    query data from this table


    I have a table with ID,RANK, WORD as columns, id and rank are ints and word
    is a text field. There are multiple entries for any number of id's.
    ex.
    1,5,word1
    1,4.3,word2
    1,2,word3
    2,5,word1
    2,4.3,word2
    2,2,word3
    etc.

    What I want to get out of it is: Show me all ID's where there is both a row
    that contains "word1" and a row that contains "word2".

    Any pointers?
    Thanks,
    Jay


  • Bill Karwin

    #2
    Re: SQL question

    Jay wrote:[color=blue]
    > What I want to get out of it is: Show me all ID's where there is both a row
    > that contains "word1" and a row that contains "word2".[/color]

    This is a slightly tricky way, that should work in either MySQL 3.x or 4.x:

    SELECT DISTINCT T1.ID
    FROM MyTable T1 INNER JOIN MyTable T2 ON T1.ID = T2.ID
    WHERE T1.WORD LIKE '%word1%' AND T2.WORD LIKE '%word2%';

    Regards,
    Bill K.

    Comment

    • news

      #3
      Re: SQL question

      "Bill Karwin" <bill@karwin.co m> wrote in message
      news:cb9v5j05u9 @enews2.newsguy .com...[color=blue]
      > Jay wrote:[color=green]
      > > What I want to get out of it is: Show me all ID's where there is both a[/color][/color]
      row[color=blue][color=green]
      > > that contains "word1" and a row that contains "word2".[/color]
      >
      > This is a slightly tricky way, that should work in either MySQL 3.x or[/color]
      4.x:[color=blue]
      >
      > SELECT DISTINCT T1.ID
      > FROM MyTable T1 INNER JOIN MyTable T2 ON T1.ID = T2.ID
      > WHERE T1.WORD LIKE '%word1%' AND T2.WORD LIKE '%word2%';
      >
      > Regards,
      > Bill K.
      >[/color]

      Thanks Bill, it doesn;t seem to work. I get a result for some queries but
      not for others.


      Comment

      • Bill Karwin

        #4
        Re: SQL question

        news wrote:[color=blue]
        > Thanks Bill, it doesn;t seem to work. I get a result for some queries but
        > not for others.[/color]

        It worked for me with the sample data you provided in your first post.
        If there are other details about your data that you're not describing,
        then there's not much I can suggest.

        Best of luck!
        Bill K.

        Comment

        • Jay

          #5
          Re: SQL question

          "Bill Karwin" <bill@karwin.co m> wrote in message
          news:cbakth01qj k@enews1.newsgu y.com...[color=blue]
          > news wrote:[color=green]
          > > Thanks Bill, it doesn;t seem to work. I get a result for some queries[/color][/color]
          but[color=blue][color=green]
          > > not for others.[/color]
          >
          > It worked for me with the sample data you provided in your first post.
          > If there are other details about your data that you're not describing,
          > then there's not much I can suggest.
          >
          > Best of luck!
          > Bill K.
          >[/color]

          Bill,

          Nope, you are right, it does work. I had 2 tables that were slightly
          different and I ran it against the wrong one.

          Many thanks,
          Jay



          Comment

          Working...