2
I have a media table with the fields id_midia
, nome
, tipo
and another table of synonyms that has the fields id_midia
, sinônimo
.
I need to make a query where it checks the two tables if the term typed is contained in the media names or in the synonym.
I’m doing it this way:
$sql = "
SELECT ayzac_midia_name,ayzac_midia_id
FROM `ayzac_midia`
WHERE `ayzac_midia`.`ayzac_midia_name`
LIKE '%".$searchTerm."%'
UNION SELECT ayzac_tag,ayzac_midia_id
FROM `ayzac_tags`
WHERE `ayzac_tags`.`ayzac_tag`
LIKE '%".$searchTerm."%'
";
Only that I need the information from the media table, and when the term found is a synonym it brings the sinônimo
instead of nome
. And also I need the type in the media table and this way it only returns me the nome
and the id
. Also, if the term sought exists in the two tables with the same id
it displays both tables and this cannot happen.
If anyone can help me with that I would be very grateful.
@Edit Explaining the context:
What I’m doing is a search of movies and series registered in my database, it turns out that the user does not necessarily need to search for the full name of the media he can search for a synonym or a tag, for example Game of Thrones can be searched for by Got, but even if he uses the term Got, I want him to return Game of Thrones.
PS: Got is in a table only for synonyms that contains only id_midia, which is a FK of the media table and the synonym.
As soon as I give I will try to do this, it looks like it will solve my problem. Then I give my feedback
– Otavio Rocha
It didn’t work, let me try to explain better: What I’m doing is a search of movies and series registered in my database, it turns out that the user doesn’t necessarily need to search for the full name of the media he can search for a synonym or a tag, for example Game of Thrones can be searched for by Got, but even if he searches using the term Got, I want to return Game of Thrones.
– Otavio Rocha
Lie, I explained everything, it worked, I was the one who ate the belly, I had to change some things, it was like this
$sql = "(SELECT ayzac_midia_name,ayzac_midia_id,ayzac_midia_name FROM ayzac_midia WHERE ayzac_midia.ayzac_midia_name LIKE '%".$searchTerm."%') UNION (SELECT ayzac_tags.ayzac_tag,ayzac_tags.ayzac_midia_id, ayzac_midia.ayzac_midia_name FROM ayzac_tags INNER JOIN ayzac_midia ON ayzac_midia.ayzac_midia_id = ayzac_tags.ayzac_midia_id WHERE ayzac_tags.ayzac_tag LIKE '%".$searchTerm."%')";
– Otavio Rocha