1

I have two tables:

users:

+-------+-------------+------+
| id    | name        | type |
+-------+-------------+------+
|   1   | Name 1      |  1   |
|   2   | Name 2      |  2   |
|   3   | Name 3      |  3   |
+-------+-------------+------+

messages:

+-------+-------------+-----------+----------+
| id    |  poster_id  |  message  | for_type |
+-------+-------------+-----------+----------+
|   1   |      3      |   mess1   |     1    |
|   2   |      2      |   mess2   |    2,3   |
|   3   |      1      |   mess3   |    3,1   |
+-------+-------------+-----------+----------+

What query for get messages on $user_type in for_type ?

SELECT messages.*, users.name FROM messages LEFT JOIN users ON 
messages.poster_id = users.id WHERE messages.id > ' . $last_message_id . ' AND 
' . $user_type . ' IN (messages.for_type) 
ORDER BY news.id ASC
Serg
  • 103
  • 1
  • 9

1 Answers1

1

If I have understood right, you want to select a particular for_type and have the problem that the column is not correctly normalised, having more than one value. If you cannot normalise the structure correctly a LIKE command in the where clause would work, for example:

for_type LIKE "%1%"

Will get record 1 and 3 from messages.

Lew Perren
  • 1,209
  • 1
  • 10
  • 14