简体   繁体   English

如何[MySQL]选择具有记录的用户

[英]How to [MySQL] select users with records

I have these tables: 我有这些表:

users : 用户

|user_id|user_name|
|-------|---------|
|   1   |  John   |
|-------|---------|
|   2   |  Mark   |
|-------|---------|
|   3   |  Paul   |
|-------|---------|

records : 记录

| record_id | user_id |    records   |
|-----------|---------|--------------|
|    1      |    3    | lorem ipsum  |
|-----------|---------|--------------|
|    2      |    3    | dolor sit    |
|-----------|---------|--------------|
|    3      |    3    | consectetur  |
|-----------|---------|--------------|
|    4      |    1    | adipisicing  |
|-----------|---------|--------------|
|    5      |    3    | eiusmod      |
|-----------|---------|--------------|
|    6      |    1    | consectetur  |
|-----------|---------|--------------|

What I want is to select users who have records. 我想要的是选择具有记录的用户。 In this case, only John and Paul should return. 在这种情况下,只有约翰保罗应该回来。

Using a subquery: 使用子查询:

SELECT user_name FROM users WHERE user_id IN (SELECT DISTINCT user_id FROM records)

Or using an INNER JOIN: 或使用内部联接:

SELECT u.user_name FROM users u
INNER JOIN records r ON u.user_id=r.user_id
GROUP BY u.user_name
SELECT u.user_name, r.records FROM users as u, records as r where u.user_id = r.user_id

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

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