View Full Version : How to refer another table in mysql within a LIKE statement
hmmduh
Feb 26, 2010, 05:16 PM
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
jmd814
Mar 1, 2010, 01:24 PM
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 (http://dev.mysql.com/doc/refman/5.0/en/delete.html)
Search for "WHERE IN"
slapshot_oi
Mar 18, 2010, 08:11 AM
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:
DELETE FROM a
WHERE field1 LIKE '%' + (
SELECT field2
FROM b
WHERE field2 = 'some filter value here'
) + '%'
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.