Results 1 to 6 of 6
  1. #1
    Join Date
    May 2012
    Posts
    3

    Question Unanswered: SQL + LIKE problem

    Hello,
    I try a query on two tables to connect each other with the "Like" - operant.

    table1: test
    table2: test2

    test.name="This is a test"
    test2.name="is a"

    Now I try to connect these tables with each other:
    Code:
    SELECT *
    FROM test,test2
    WHERE test.id=test2.id AND test2.name LIKE ('%'+test.name+'%')
    The problem is now that the front part ("This") is ignored, but the rear part ("test") not. And so the query doesn't match to any result.

    does anyone have an idea?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    change this --
    Code:
    AND test2.name LIKE ('%'+test.name+'%')
    to this --
    Code:
    AND test.name LIKE '%'+test2.name+'%'
    note: the parentheses are not required and should be removed

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    May 2012
    Posts
    3
    I change my code to:
    Code:
    AND (test.name LIKE '%'+test2.name+'%' OR test2.name LIKE '%'+test.name+'%')
    Same problem

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    in that case, perhaps the problem lies here ...
    Code:
    test.id=test2.id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    May 2012
    Posts
    3
    Mh I removed the first command but the problem still exists
    But it's very strange that the front part ("This") is ignored, but the rear part ("test") not

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    It seems pretty straightforward to me. LIKE isn't reflexive, so "%is a%" is like "this is a test" but "%this is a test%" is not like "is a".

    What am I missing???

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

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