1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Join MySQL tables using LIKE?

Discussion in 'MySQL' started by ironbark, Jan 15, 2009.

  1. #1
    I have a requirement to join to table where the values in the match columns are a subset of one another as per example below.

    Table 1 (T1) has key values of format:
    apple
    banana
    orange

    Table 2 (T2) has values of format:
    apple 123
    yellow banana
    123456 orange
    the orange tree

    What I want is a query that matches the T1 value with corresponding substring in T2 giving the following result:
    apple, apple 123
    banana, yellow banana
    orange, 123456 orange
    orange, the orange tree

    I'm trying to join using LIKE but haven't figured out the syntax. Is there an easy way to do this.
     
    ironbark, Jan 15, 2009 IP
  2. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    [COLOR="Blue"]select [/COLOR]
    	t1.id, [COLOR="Green"]-- assming this is the id field holding the apple text[/COLOR]
    	t2.[text] [COLOR="Green"]-- assuming this is the fieldname holding the apple 123 text..[/COLOR]
    [COLOR="Blue"]from [/COLOR]
    	t1 [COLOR="SlateGray"]inner join [/COLOR]t2 [COLOR="Blue"]on [/COLOR]
    	t2.text [COLOR="SlateGray"]like [/COLOR][COLOR="Red"]'%'[/COLOR] + t1.id + [COLOR="Red"]'%'[/COLOR]
    Code (markup):
    note that this kind of join is not very efficient .. (get worse the more the data are..)

    take care
     
    gnp, Jan 15, 2009 IP
  3. ironbark

    ironbark Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, I ran a test through phpMyadmin and got the following error. Any ideas what is wrong?

    SELECT t1.id, t2.text
    FROM t1
    INNER JOIN t2 ON t2.text LIKE '%' + t1.id + '%'
    LIMIT 0 , 30

    MySQL said: Documentation
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+ t1 . id + '%'
    LIMIT 0, 30' at line 1
     
    ironbark, Jan 15, 2009 IP
  4. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hmm..

    try changing the
    [COLOR="SlateGray"]like [/COLOR][COLOR="Red"]'%'[/COLOR] + t1.id + [COLOR="Red"]'%'[/COLOR]
    Code (markup):
    to
    [COLOR="SlateGray"]like [/COLOR]concat([COLOR="Red"]'%'[/COLOR] , t1.id , [COLOR="Red"]'%'[/COLOR])
    Code (markup):
    also make sure you alter the query to match the names of your fields...
     
    gnp, Jan 15, 2009 IP
  5. ironbark

    ironbark Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    perfect ! - thanks

    id text
    apple apple 123
    banana yellow banana
    orange 123456 orange
    orange the orange tree
     
    ironbark, Jan 15, 2009 IP