How To Store Tags In A Database Using MySQL and PHP?

前端 未结 3 1468
半阙折子戏
半阙折子戏 2021-02-10 22:31

I wanted to create a database that will store the tags that users enter for their questions and then display them all for each individual question posted; something like here on

3条回答
  •  悲&欢浪女
    2021-02-10 23:01

    Tables:

    CREATE TABLE questions_tags (
    tag_id INT INT NOT NULL,
    question_id INT NOT NULL,
    );
    
    CREATE TABLE tags (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    tag VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
    );
    

    SQL

    Select the tag related to a question

    SELECT     tag_id
    FROM         question_tags
    WHERE     question_id = $yourquestionid'
    

    Select the questions related to a tag

    SELECT     question_id
    FROM         question_tags
    WHERE     tag_id = $yourtagid'
    

    Count how many times a tag has been used

    SELECT COUNT(tag_id) 
    FROM question_tags 
    where tag_id=$yourtagid
    

    Make a tag cloud

    SELECT COUNT(tag_id)
    FROM question_tags 
    GROUP BY tag;
    

提交回复
热议问题