sql - 如何获取postgres中的字段计数?
问题描述
我有一张表orders
,其结构如下:
create table orders (id serial primary key, created_at timestamp, updated_at timestamp, product_id varchar[]);
我对于 product_id 我正在插入这样的数据:
insert into orders values (1,now(),now(),'{"1","2"}');
product_id
保存以特定顺序订购的不同产品。products
表中的每个条目都有一个条目product_id
。
现在我想获取product_id
特定订单的计数。
示例:对于我所做的上述插入,我应该得到如下内容:
Id Count
1 2
postgresql怎么可能?
编辑:很抱歉弄乱了create table
查询。这是一个 varchar[] 而不是 varchar(255)
解决方案
您可以将值转换为数组并测量值的数量:
select o.*, cardinality(product_id)
from orders o
推荐阅读
- javascript - 如何使用部分视图正确添加分页
- loops - Dart - 关于在 if 语句中执行多行处理
- heroku - 使用github在heroku中部署项目
- azure - Azure Cosmos db Gremlin elementMap()
- ios - 关于重新排序 tableView 的问题(Apple Swift 和 Xcode 11)
- javascript - 如何使用 RecordRTC-over-Socketio 将麦克风流从客户端传递到服务器
- matplotlib - 网格规范和颜色条的问题
- caching - 使用 Zookeeper 和 Redis 的分布式索引
- eclipse - Eclipse 无头应用程序因 Stackoverflow 错误而失败
- node.js - 停止调试器后 VS Code 调试器不会终止节点进程