如何在表之间建立关系?

I have one table for posting new articles and the content of the table is printed using a while loop.

Now I want the viewers to be able to comment on the article so I created another table for comments and a form under article in the while loop, but whenever anybody comments on the article the comment is showed with all the articles.

Because article and comments are in one while loop and there is no relationship between the tables. tables are as follows:

content_posts( id int primary auto, subject varchar, postby varchar, date date, content varchar )

comments( id int primary auto, name varchar, email varchar, body varchar )

You can try linking the tabels by the aticle id column: for example: ARTICLES with the column id and COMMENTS with a field named article_id which will contain the id of the corresponding article. Then you can use an INNER JOIN like this

SELECT * FROM ARTICLES as t1 INNER JOIN COMMENTS as t2 ON t1.id = t2.article_id

Hope it helps!

You need to define primary key in your article table, normally called id, and a foreign key in your comments table. This key relationship, in this case a one to many, is then used to ensure only comments related to a specific article are displayed. Using the id you would loop through the foreign keys finding the matches.

If you want to set up a relationship you have to determine the sense of your relationship.

One to One

Each side of the relationship is related to one and only entity. For example, nowaday, you split up the users definition in two parts :

  • the connection handler (password, last login date, activation)
  • the profile info (username, email, favourite movie...)

One profile has only one handler and one handler has only one profile.

In that case each table has to be added with a foreign key that takes the identifier of each entity.

One to Many

The case that you have with your blog. One article has many comments, one comments has one and only one article.

So, in your comment table, you have to add your foreign key that is a link to your article.id. Then to select the comment that are in one article, you have to make a WHERE :

SELECT * FROM comment_table WHERE id_article = 1 -- for your first article, 

If you want to load info from your article table and your comment table, you have to use the JOIN command :

SELECT comment_table.*, article_table.* FROM article_table 
   JOIN comment_table ON article_table.id = comment_table.id_article 

many to many

each entity can be associated to many of the other. For example, il you provide tags to your articles : each tag cas be assigned to many articles, each article can have many tags.

In this case, you have to create a new table which only have tag_id and article_id. To select one article with his tags, you have to make a double join :

SELECT article.*, tags.* FROM article_table article 
    JOIN tags_in_article tia ON tia.id_article = article.id
    JOIN tag_table tags ON tia.id_tag = tag.id
    WHERE article.id = 1