I have 3 tables in the database:

  1. Articles (spisok) - 2 fields: id , text
  2. Tags (tags) - 2 fields: id , tag
  3. Link (link) - 2 fields: spisok_id , tags_id

I want to generate a universal table of the output of the article itself and under it a list of tags for it. How to implement this table of relationships? How to search and display several tags related to the page?

I would really appreciate any help? I would like the php code itself ... I'm new and in pseudocode is not strong ...

And what type should be the data in all tables?

  • the problem is that you cannot understand why you need 3 tables and how to derive what you need? All the information you have, you just need to move a little. Write what you did to work? So someone will hardly write the code here. - Artem
  • you have a table for this> 3) Link (link) -2 fields: spisok_id, tags_id and ... - Artem
  • Well, for example spisok_id tags_id 1 2 2 1 1 3 well, I don’t know the syntax how to write it ... I understand ... what you need, for example, if an article with id = 1 then check how many times it occurs 1 and output the corresponding tag with the number tags_id. .. - qwerty17

2 answers 2

1st request

 SELECT * FROM `spisok` WHERE `id`=ТВОЙИДСТАТЬИ 

2nd request

 SELECT * FROM `tags`, `link` WHERE `link`.`spisok_id`=ТВОЙИДСТАТЬИ AND `link`.`tags_id`=`tags`.`id` 

Start experimenting with phpmyadmin or some other tool that allows you to simplify queries to the database, for example, HeidiSQL, compared with the command line.

    Let's do it this way:

    There will not be a third table with links, but we will list in the third field ( tags ) articles with tags that this article corresponds to, and an example of this in the attached image.

    It will be easier to disassemble, less hassle with the tables and the load on the base.

    alt text

    • It is interesting! Thank you, Andrei Arshinov! And what the truth will be less loading on base? I understand correctly that this method is more rational ???? - qwerty17
    • Not much, but more rational way. - Andrei Arshinov
    • and what type of tags field in the 'articles' table? I understand that they will be listed in phpmyadmine? 1,2,3 ... comma separated? - qwerty17
    • You can write varchar and choose a length of 30-40 characters approximately, tag identifiers will be listed separated by commas. - Andrey Arshinov
    • Here is the material for which I studied: php.net/manual/ru . Now I can not write the code, a little busy. - Andrey Arshinov