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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.