简体   繁体   English

mysql:从两个表中获取数据

[英]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% ". 我需要通过这两个表中的一个查询来获取条件为“ 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

Try with UNION . 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%')

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. 我不知道在您的特定情况下,“用户”和“成员”之间有什么区别,但这听起来似乎最好是只包含一个表“ people”,并在其中指定一个位列用户或成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM