Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   MySQL (https://www.askmehelpdesk.com/forumdisplay.php?f=442)
-   -   How to refer another table in mysql within a LIKE statement (https://www.askmehelpdesk.com/showthread.php?t=452094)

  • Feb 26, 2010, 05:16 PM
    hmmduh
    How to refer another table in mysql within a LIKE statement
    I have two tables and I want to delete rows in one table1 based on all the data in field2 of another table2 found in field1 of table1.

    I believe the solution to be like this

    DELETE FROM table1 WHERE field1 LIKE '%'+ table2.field2 +'%'

    This is not working... WHY? Is there another option.

    I want to delete rows of a table based on my keywords in table2
  • Mar 1, 2010, 01:24 PM
    jmd814

    My first instinct is to have you try a DELETE... WHERE IN (SELECT [from second table])
    But on the MySQL documentation there was a comment that it wasn't working and he came up with his own solution.
    Instead of reposting, go here to see what he did:

    MySQL :: MySQL 5.0 Reference Manual :: 12.2.2 DELETE Syntax

    Search for "WHERE IN"
  • Mar 18, 2010, 08:11 AM
    slapshot_oi
    Quote:

    Originally Posted by hmmduh View Post
    DELETE FROM table1 WHERE field1 LIKE '%'+ table2.field2 +'%'

    This is not working...WHY? Is there another option.

    A simple dot-reference won't be recognized by the SQL parser. You need a sub-query:
    [CODE=SQL]
    DELETE FROM a
    WHERE field1 LIKE '%' + (
    SELECT field2
    FROM b
    WHERE field2 = 'some filter value here'
    ) + '%'
    [/CODE]
    This code does the same thing as jm814's method, except his where clause sub-query returns an entire result set, mine just returns one value. I code like this because it's so verbose it's self-documenting.

  • All times are GMT -7. The time now is 11:03 PM.