Reputation: 8179
Table : frei_session
id username accountId status status_mesg
14 Sumit Bijvani 50 0 I am available
16 Dilip Borad 49 1 I am available
15 Karan Bijvani 51 1 I am available
Table : users
accountId friends
49 50,52
50 49,52,51,44
51 50
I have 2 tables, frei_session have records of online users and users table have data of users and friends of them separated ID by comma.
I want to retrieve data from frei_session table based on friends column of users table.
For Ex
If user 49
is online, I want Output like below
id username accountId status status_mesg
14 Sumit Bijvani 50 0 I am available
Because User 50 and 52 is friend of User 49 but now only User 50 is online
I have tried following query on single table but don't know how to do with 2 tables
SELECT DISTINCT status_mesg,username,session_id,status,guest
FROM frei_session
WHERE accountId!=49
AND guest=0
AND status!=2
AND status!=0
Upvotes: 1
Views: 133
Reputation: 263693
Your table is not normalize an considered as bad schema design. Anyway, to answer your question directly, MySQL
has a builtin function called FIND_IN_SET
which searches for a specific value on a CSV format.
SELECT b.*
FROM users a
INNER JOIN frei_session b
ON FIND_IN_SET(b.accountID, a.friends) > 0
WHERE b.status = 0 AND
a.accountID = 49
Upvotes: 3