1
I need to relate data according to the last item found in the user table, for example:
Table:
+----------------+-------------+
| user_id | title_id |
+----------------+-------------+
| 154138746 | 123 |
+----------------+-------------+
Table
+----------------+----------------------+
| title_id | genres |
+----------------+----------------------+
| 123 | Ação, Aventura, Drama|
+----------------+----------------------+
| 122 | Drama, Sci-fi |
+----------------+----------------------+
| 126 | Romance, Aventura |
+----------------+----------------------+
| 135 | Ação , Drama |
+----------------+----------------------+
What I need is to make a SELECT in table A by recovering its respective data in table B, and then using such data recover from the same table B its similar based on category, it is possible to do everything in a query or it is better to split?
Preferably I would like the query to return only the semantic values found in the tabela_B, ignoring the others that were used only as parameters for the query.
I am trying with the following query:
SELECT wt.user_id, wt.title_id,
fl.id, fl.genres,
sm.*
FROM watchedtitles wt
INNER JOIN filmes fl
ON wt.title_id = fl.id
LEFT JOIN filmes sm
ON FIND_IN_SET(SUBSTRING_INDEX(fl.genres, ',', 1), fl.genres)
In the case of watchedtitles it is my table_A from where I will pick the last item that the user watched and then search in the table_B movies, their similar ones, but when using SUBSTRING_INDEX I am limited to having to every time type separate index. Bearing in mind that the genres column is a string containing the categories separated by comma, how could such a query?
Instead of column types you could put an example of value?
– Costamilam
@Guilhermecostamilam added some values to the tables
– Leo Letto
Try with
REGEXP CONCAT('(', REPLACE(fl.genres, ', ', '|'), ')')
, this should create a regex of the type(Ação|Aventura|Drama)
, you may have to make some adaptations– Costamilam
Could you give me an example of what such an adaptation would look like?
– Leo Letto
I can’t test sql now, but the idea is to transform the column value
genres
in a regex that can be used to look for similar movies, it would look something likeLEFT JOIN filmes sm ON sm.genres REGEXP CONCAT('(', REPLACE(fl.genres, ', ', '|'), ')')
, but like I said I can’t test to make sure it works– Costamilam
It seems to work well, it would be possible to make an ORDER BY by the results containing more common categories?
– Leo Letto
I can’t think of any simple way to do this, but to get around the problem you can let the user define their favorite categories and sort by them
– Costamilam