sql - 类似 Linq 的 group by for sql
问题描述
似乎 sqlgroup by
更多的是聚合函数(COUNT、MAX、MIN、SUM、AVG)。
select count(Id), Country
from Customer
where Country <> 'CountryX'
group by Country
但是我们是否有一个类似 linq 的查询,我们想要返回按某个列分组的所有结果,例如在 linq 中我会做
id | title | category | email
------------------------------------------
1 | tname-1 | cat1 | test@example.com
2 | tname-2 | cat1 | test1@example.com
3 | tname-3 | cat2 | TEst@example.com
林克group-by
:
var groupedBy = list.GroupBy(item => item.Email);
甚至进行一些比较
var groupedBy = list.GroupBy(item => item.Email, StringComparer.OrdinalIgnoreCase);
结果将是这样的:
key | items
----------------------------------------------------------------------------------------------
test@example.com | [{Id :1, Title : "tname-1", category: "cat1", email: "test@example.com" },{Id :3, Title : "tname-3", category: "cat2", email: "TEst@example.com" } ]
test1@example.com| [{Id :2, Title : "tname-2", category: "cat1", email: "test1@example.com" }]
但是使用 sql 我肯定只想返回列的子集,id
比如title
和email
。
解决方案
推荐阅读
- node.js - react-dropzone-uploader AVI 文件停留在“准备中”状态
- python - 使用 Python GTK+ 3 将对话框按钮居中对齐
- mysql - 发出套接字,但数据未显示在高图表中
- angular - 角度 matDatepicker 没有 mat-form-field
- google-cast - 如何测试多个版本的投射接收器应用程序
- javascript - 如何通过字符串中“单词”的升序对字符串数组进行排序?javascript
- android - 如何选择性地观察 Android Room 数据库 INSERT、DELETE 和 UPDATE 事件?
- c++ - g++ 不会在模板代码中发出 -Wsign-compare
- javascript - 如何让我的函数上限(x)在命令提示符下生成一个数字?
- javascript - 为什么在尝试搜索二叉树时出现无限循环?