mysql - Getting similar rows from Database Table -


hello friends having following table structure of quote table

enter image description here

i want access similar quotes (which have same author_id , category_id) particular _id(quoteid)

similar quotes means columns of rows of table of same category_id , same author_id. 2 quotes can considered similar if authors same , category same.

same author id , category id?

select `related_quote`.* `quote` `main_quote` left join `quote` `related_quote` using(`author_id`, `category_id`) `main_quote`.`_id` = quote_id 

this original quote (ie. quote_id), , related it.

mysql> select `related_quote`.*     -> `quote` `main_quote`     -> left join `quote` `related_quote` using(`author_id`, `category_id`)     -> `main_quote`.`_id` = 1; +------+----------------+-----------+-------------+ | _id  | content        | author_id | category_id | +------+----------------+-----------+-------------+ |    1 | test           |         1 |           1 | |    2 | test related   |         1 |           1 | |    3 | test related 2 |         1 |           1 | +------+----------------+-----------+-------------+ 3 rows in set (0.01 sec) 

you can remove quote_id resultset adding

and `related_quote`.`_id` != quote_id 

to end of query.


Comments