How to consult with Where and like between two tables?

Asked

Viewed 438 times

0

Guys, I have the following problem:

I have two tables, the following tables :

tabela mensagens
+----+-------------------+-------+---------------------+
| id | mensagem          | _de   | hora                |
+----+-------------------+-------+---------------------+
| 1  | teste de mensagem | tiago | 2015-12-04 14:56:41 |
+----+-------------------+-------+---------------------+
tabela amigos
+----+--------+--------------+
| id | user   | amigos       |
+----+--------+--------------+
| 1  | italos | igors;tiago; |
+----+--------+--------------+

I would like to make the following query: that I receive the result of all messages only if there exists in table friends, the value of _de of the table messages. Thank you all.

2 answers

3

You need a table to link, do searches with varchar even more with multiple values, can make your query heavy and slow for no reason.

tabela mensagens
+----+-------------------+---------------------+
| id | mensagem          | hora                |
+----+-------------------+---------------------+
| 9  | teste de mensagem | 2015-12-04 14:56:41 |
+----+-------------------+---------------------+

tabela amigos
+----+--------+
| id | user   |
+----+--------+
| 1  | italos | 
+----+--------+

tabela amigosItem
+----+--------+--------+--------------+
| id | user   | msg    |  amigos      |
+----+--------+--------+--------------+
| 5  | 1      | 9      | igors        |
+----+--------+-----------------------+
| 6  | 1      | 9      | tiago        |
+----+--------+-----------------------+

Select A.mensagem 
from amigos A
 Inner join amigosItem AI On A.id = AI.user
 Inner join mensagens M On M.id = AI.msg

Would look like this.

0

Come on Wesley, first the field 'friends' of table friends should be a new table in the database (friend_item), it is not advisable for you to have a multivariate attribute in a table. A solution to select the way you want, is to use the INNER JOIN. But it will only work if your bank is in all relationships correctly. You can generate a script from the DB structure and send it here?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.