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

前端 未结 6 1249
执笔经年
执笔经年 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 22:51

    1. This mapping-table concept is pretty standard and looks well-implemented here. The only thing I'd change is getting rid of the ID in Table 2; for what would you use it? Just make a joint key for Table 2 on both item ID and tag ID.

    2. Actually, selecting where an item matches ALL tags is hard. Try this:

      SELECT item_id,COUNT(tag_id) FROM Table2 WHERE tag_id IN (your set here) GROUP BY item_id

    Where the count equals the number of tag IDs in your set, you have found a match.

提交回复
热议问题