I am trying to create a MySQL subquery that will retrieve the records from table1, only if the records from table2 do not exist anywhere within the table1 record. Here is my schema:
CREATE TABLE table1 (
CompanyName VARCHAR(20)
);
CREATE TABLE table2 (
ExcludeName VARCHAR(20)
);
INSERT INTO table1 VALUES
( 'COMPANY AAA 111' ),
( 'COMPANY BBB 222' ),
( 'COMPANY CCC 333' ),
( 'COMPANY DDD 444' ),
( 'COMPANY EEE 555' );
INSERT INTO table2 VALUES
( 'BBB' ),
( 'DDD' );
And here is my SQL:
SELECT DISTINCT t1.CompanyName
FROM table1 t1
JOIN table2 t2
ON INSTR(t1.CompanyName, t2.ExcludeName ) = 0;
As shown in this SQL fiddle, I am not getting the correct result. I want the query to return only COMPANY AAA 111, COMPANY CCC 333 and COMPANY EEE 555, but instead the query is returning all of the records. Why aren't the BBB and DDD records being excluded?