Skip to content

Instantly share code, notes, and snippets.

@irohiroki
Created December 15, 2010 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irohiroki/742374 to your computer and use it in GitHub Desktop.
Save irohiroki/742374 to your computer and use it in GitHub Desktop.
mysql> select * from items;
+------+----+
| name | id |
+------+----+
| aaa | 1 |
| bbb | 2 |
| ccc | 3 |
| ddd | 4 |
+------+----+
4 rows in set (0.00 sec)
mysql> select * from tags;
+------+----+
| name | id |
+------+----+
| tag1 | 1 |
| tag2 | 2 |
+------+----+
2 rows in set (0.00 sec)
mysql> select * from items_tags;
+---------+--------+
| item_id | tag_id |
+---------+--------+
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
| 3 | 2 |
+---------+--------+
4 rows in set (0.00 sec)
mysql> select i.name, t1.name, t2.name from items i join items_tags it1 on it1.item_id = i.id join tags t1 on it1.tag_id = t1.id join items_tags it2 on it2.item_id = i.id join tags t2 on it2.tag_id = t2.id where t1.name = 'tag1' and t2.name = 'tag2';
+------+------+------+
| name | name | name |
+------+------+------+
| bbb | tag1 | tag2 |
+------+------+------+
1 row in set (0.00 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment