Will this (normalised) database structure permit me to search by tags as I intend?

前端 未结 6 1254
执笔经年
执笔经年 2020-12-16 22:24

I am trying to set up a normalised MySQL database containing the three following tables. The first table contains a list of items which can be described by various tags. T

6条回答
  •  一向
    一向 (楼主)
    2020-12-16 23:00

    You could try something like this:

    select item, count(*) 'NrMatches'
    from #table1 i
    inner join #table2 l ON i.id = l.item_id
    inner join #table3 t on l.tag_id = t.id
    where t.tag IN ('cheap', 'pet', 'dog')
    group by item
    having count(*) = (select count(*) from #table3 
                       where tag IN ('cheap', 'pet', 'dog'))
    

    It means having your search terms twice, but it mostly does what you're after.

提交回复
热议问题