lnguyen55
lnguyen55

Reputation: 737

MySQL IN operator of result from another column

I have 2 tables:

users(uid, name, titles) titles(uid, name)

users:

uid | name | titles 1 David 2,4 2 John 5 3 Jane 4

titles:

uid | name 2 Owner 4 CEO 5 Manager

The question is how do I select something like this:

SELECT u.* FROM users as u JOIN titles as t ON t.uid IN (u.titles) WHERE t.uid=2

Notice the IN(u.titles)? It's only taking the first title uid in u.titles field. That means when I change condition to WHERE t.uid=4, it shows no records.

Any idea?

Upvotes: 0

Views: 25

Answers (2)

Sebastian
Sebastian

Reputation: 11

If you want that each user can have multiple titles I would recommend a reference table which references the users to the titles.

Upvotes: 0

juergen d
juergen d

Reputation: 204766

SELECT u.* 
FROM users as u
JOIN titles as t ON find_in_set(t.uid, u.titles) > 0
WHERE t.uid=2

Upvotes: 1

Related Questions