lolalola
lolalola

Reputation: 3823

mysql: get data from two tables

i have two tables "members" and "users".

I need with one query from this two tables get all users where condition is "name LIKE %Joy%".

How join in this situation two tables?

Tables:
users
id / name / age
1 joy 15
2 marko 26

members
id / name / level
1 peter 1
2 joyes 0
3 marko 1

Upvotes: 2

Views: 977

Answers (2)

vbence
vbence

Reputation: 20333

Try with UNION. I added the first column so you can check later where that result came from (and create a link to the user's profile page for example).

(SELECT 'user' AS type, id, name FROM user WHERE name LIKE '%Joy%')
UNION
(SELECT 'member', id, name FROM member WHERE name LIKE '%Joy%')

Upvotes: 4

Mels
Mels

Reputation: 476

It appears as though both tables essentially store information about the same kind of thing: people. I don't know what the difference is between a "user" and a "member" in your specific situation, but it sounds as though you might be better off having just one table "people" with a bit column specifying whether the person is a user or a member.

Upvotes: 1

Related Questions