Best DB (MySQL) structure: Articles which contain favored tags

前端 未结 2 1738
再見小時候
再見小時候 2021-01-01 05:59

I\'ve built a news site: - The articles are shown on the front page ordered by date. The newest one first. - The news are in the table \"news\" with the fields \"id\", \"t

2条回答
  •  独厮守ぢ
    2021-01-01 06:01

    The following is by no means exhaustive/definitive, but it should get you going in the right direction.

    Tables:

    news
    =====
    id
    title
    text
    
    tag
    ===
    id
    tag
    
    tag_map
    =======
    tag_id
    news_id
    
    favorite_tags
    =============
    user_id
    tag_id
    

    Query

    SELECT * 
    FROM favorite_tags
    JOIN tag_map ON favorite_tags.tag_id = tag_map.tag_id
    JOIN news ON tag_map.news_id = news.id
    WHERE favorite_tags.user_id = $userid
    

提交回复
热议问题