Created
April 6, 2016 10:19
-
-
Save hollodk/d1fa00aff24553f16b510047edf5c2b5 to your computer and use it in GitHub Desktop.
done commando sql stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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