Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The db part is easy. This is just a sample so you can see how db can look like, not any particular SQL engine queries.</p> <pre><code>CREATE TABLE posts ( id INT PRIMARY KEY, subject VARCHAR(100), body TEXT ) CREATE TABLE tags ( id INT PRIMARY KEY, name VARCHAR(50) ) CREATE TABLE post_tags ( post_id INT, tag_id INT, FOREIGN KEY (post_id) REFERENCES posts (id), FOREIGN KEY (tag_id) REFERENCES posts (id) ) </code></pre> <p>To get items with <code>yourTag</code> tag you will just run query like this</p> <pre><code>SELECT P.* FROM posts P LEFT JOIN post_tags PT ON (PT.post_id = P.id) LEFT JOIN tags T ON (T.id = PT.tag_id) WHERE T.name = 'yourTag'; </code></pre> <p>To get tags associated with post with id of <code>123</code> you run this query:</p> <pre><code>SELECT T.* FROM tags T LEFT JOIN post_tags PT ON (T.id = PT.tag_id) LEFT JOIN posts P ON (PT.post_id = P.id) WHERE P.id = 123; </code></pre> <p>For the PHP part you could use a framework. Many (if not all) frameworks can easily model such relationships. For example in CakePHP this done like that:</p> <pre><code>class Post extends AppModel { $useTable = 'posts'; $hasAndBelongsToMany = array( 'Tag' =&gt; array( 'className' =&gt; 'Tag' 'joinTable' =&gt; 'post_tags' 'foreignKey' =&gt; 'post_id' 'associationForeignKey' =&gt; 'tag_id' ) ); } class Tag extends AppModel { $useTable = 'tags'; $hasAndBelongsToMany = array( 'Post' =&gt; array( 'className' =&gt; 'Post' 'joinTable' =&gt; 'post_tags' 'foreignKey' =&gt; 'tag_id' 'associationForeignKey' =&gt; 'post_id' ) ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload