mysql - SQL查询获取具有重复数据的表的N个元素
问题描述
我有一个以这种格式返回数据的查询
| Name | SomeData | MoreStuff |
|--------|-------------|---------------|
| asset1 | I need this | And also this |
| asset1 | I need this | And also this |
| asset1 | I need this | And also this |
| asset2 | I need this | And also this |
| asset2 | I need this | And also this |
| asset3 | I need this | And also this |
| asset3 | I need this | And also this |
| asset3 | I need this | And also this |
| asset4 | I need this | And also this |
| asset5 | I need this | And also this |
| asset5 | I need this | And also this |
| ...... | ........... | ............. |
假设我需要 20 个不同的资产,还需要每一行的数据。这里的“LIMIT”不起作用,“GROUP BY”也不起作用。
我还有什么其他选择?
- - - 编辑 - -
例如,如果我需要 3 个不同的资产,则输出应该是
| Name | SomeData | MoreStuff |
|--------|-------------|---------------|
| asset1 | I need this | And also this |
| asset1 | I need this | And also this |
| asset1 | I need this | And also this |
| asset2 | I need this | And also this |
| asset2 | I need this | And also this |
| asset3 | I need this | And also this |
| asset3 | I need this | And also this |
| asset3 | I need this | And also this |
解决方案
您可以通过对同一个表进行联接来获得所需的结果集,但行数有限,例如
select a.*
from demo a
join (
select distinct Name
from demo
order by Name
limit 3
) b on a.Name = b.Name
推荐阅读
- c++ - 为什么我在函数中创建的对象不会被其他函数修改?(C++)
- python - 检索解包需要8字节的缓冲区错误套接字python
- github - 有什么方法可以使用 Google Colab 在 GitHub 上发送 Pull Requests 和/或 Merge PRs?
- android - Flutter - 错误:此库已禁用空安全功能
- php - laravel 8 中的单元测试控制器
- rendering - 在虚幻引擎 4 中使用自定义组件渲染 obj 网格会呈现视觉伪影
- c# - 用户控件不更新文本框 mvvm WPF C#
- javascript - 预加载器屏幕淡出后播放 embed youtube
- python-3.x - Tensorflow 2.2.0-rc4 AttributeError:模块'tensorflow.compat.v1'在Spyder中没有属性'contrib'?
- c# - 如何增加分数并在控制台中打印?