Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从每行查询结果中选择数据_Sql_Postgresql - Fatal编程技术网

Sql 从每行查询结果中选择数据

Sql 从每行查询结果中选择数据,sql,postgresql,Sql,Postgresql,例如,我有一个事务表 id_transaction|description|id_operator 1 |foo |21 2 |fooo |21 1 |oof |20 还有像这样的操作员表 id_operator|operator_name 20 |op1 21 |op2 如何查询操作员id为20和21的事务量?我想检索类似这样的数据 id_oper

例如,我有一个事务表

id_transaction|description|id_operator
1             |foo        |21
2             |fooo       |21
1             |oof        |20
还有像这样的操作员表

id_operator|operator_name
20         |op1
21         |op2
如何查询操作员id为20和21的事务量?我想检索类似这样的数据

id_operator|count
20         |1
21         |2
我试过这个问题

select count(*) from "transaction" where id_operator in (
    select id_operator from "operator" where "operator_name" like 'op%'
);
这个问题呢

select count(DISTINCT(trt.id_operator)) from "transaction" trt
join "operator" mso on trt.id_operator = mso.id_operator
and mso."operator_name" like 'op%';
但是这些查询返回op1和op2中的所有数据,而不是每个运算符的数据。。
我如何才能做到这一点?

您应该查看
分组依据
子句

对于你的情况,我想你需要

select id_operator,count(id_transaction)
from "transaction"
group by id_operator

使用
分组依据

select mso.operator_name,count(trt.id_operator) 
FROM transaction trt
INNER JOIN operator mso on trt.id_operator = mso.id_operator
GROUP BY mso.operator_name;

不相关,但:
不相关
不是一个函数
distinct(trt.id\u操作符)
distinct trt.id\u操作符相同
提示:使用
groupby
@honeybacker是,groupby执行任务。。谢谢他不需要加入,他所有的操作员ID都在同一个表(1-1)中,您通常按所选列分组,除了那些设置函数的参数。@MKougiouris OP需要通过
操作符获取
计数。操作符_name
@D-Shih他明确表示需要这样的数据:id_操作符|计数20 | 1 21 | 2,但可能我遗漏了什么!