Skip to content

Instantly share code, notes, and snippets.

@hollodk
Created April 6, 2016 10:19
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 hollodk/d1fa00aff24553f16b510047edf5c2b5 to your computer and use it in GitHub Desktop.
Save hollodk/d1fa00aff24553f16b510047edf5c2b5 to your computer and use it in GitHub Desktop.
done commando sql stuff
mysql> select * from product; select * from category; select * from tag; select * from product_tag; +----+-----------+-------------+
| id | name | category_id |
+----+-----------+-------------+
| 1 | shampoo | 1 |
| 2 | skocreme | 1 |
| 3 | vase | 2 |
| 4 | hundesnor | 1 |
+----+-----------+-------------+
4 rows in set (0.00 sec)
+----+----------+
| id | name |
+----+----------+
| 1 | mad |
| 2 | beverage |
+----+----------+
2 rows in set (0.01 sec)
+----+------+
| id | name |
+----+------+
| 1 | hund |
| 2 | kat |
| 3 | and |
+----+------+
3 rows in set (0.00 sec)
+----+------------+--------+
| id | product_id | tag_id |
+----+------------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 3 |
| 5 | 2 | 2 |
| 6 | 3 | 1 |
| 7 | 3 | 2 |
| 8 | 3 | 3 |
| 9 | 4 | 3 |
| 10 | 4 | 2 |
| 11 | 4 | 1 |
+----+------------+--------+
11 rows in set (0.00 sec)
mysql> select count(*) cnt, pt.product_id from product_tag pt left join product p on pt.product_id=p.id where pt.tag_id in (select t1.tag_id from product_tag t1 where t1.product_id=1) and p.category_id=(select t2.category_id from product t2 where t2.id=1) and product_id <> 1 group by pt.product_id order by cnt desc;
+-----+------------+
| cnt | product_id |
+-----+------------+
| 3 | 4 |
| 2 | 2 |
+-----+------------+
2 rows in set (0.01 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment