gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:37:30 24 4
gpt4 key购买 nike

我有这些表:

用户:

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

记录:

| 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 |
|-----------|---------|--------------|

我想要的是选择有记录的用户。在这种情况下,只有 JohnPaul 应该返回。

最佳答案

使用子查询:

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

或者使用 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

关于mysql - [MySQL]如何选择有记录的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656990/

24 4 0
文章推荐: php - 根据特定条件将来自 my_MySQL 的值分组显示在单独的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com