Search code examples
mysqljoinsql-like

SQL Join with not like


I'm on MySQL 5.0.

I basically have a List Item table and a List Title table. For each Title, there can be 60+ items, which are all text. Dynamically I am trying to get Titles returned that have items people want to include or exclude. My Query is basically:

Select Distinct TitleID from Titles
left join Items on Items.titleID = Titles.titleID
Where Items.Name not like 'Item 2'

If any Items have Name like 'Item 2' then I don't want the TitleID. However there could be 59 other Names associated with a Title, so this query is still returning Every possible TitleID.

How can I write it so that I only get the TitleIDs I'm looking for?


Solution

  • Select Distinct TitleID from Items
    Where TitleID not in (
    Select TitleID from Items
    Where Name like 'Item 2')
    

    There is no wildcard in 'item 2' though, so it does not make much sense.