刚刚安装了 9.4 并尝试使用 JSONB 字段类型。
我制作了一个带有 jsonb 字段的表格,并且可以从中进行选择:
select statistics->'statistics'->'all_trades'->'all'->'all_trades_perc_profit' as profitable_perc FROM trade_statistics
Run Code Online (Sandbox Code Playgroud)
工作正常。
现在我想根据字段值过滤结果:
select statistics->'statistics'->'all_trades'->'all'->'all_trades_perc_profit' as profitable_perc FROM trade_statistics WHERE profitable_perc > 1
//There is no "profitable_perc" column
Run Code Online (Sandbox Code Playgroud)
不起作用。
如果我尝试将结果转换为双精度,也不起作用。
select cast(statistics->'statistics'->'all_trades'->'all'->'all_trades_perc_profit' as double precision) as profitable_perc FROM trade_statistics
//cant convert jsonb into double precision
Run Code Online (Sandbox Code Playgroud)
在 jsonb 的情况下,我应该如何在 WHERE 子句中使用选择结果?
必须进行三处更正:
SELECT在WHERE子句中引用列表别名->>运算符以文本形式获取值将文本值转换为整数,以便进行比较
SELECT *
FROM (
SELECT (statistics->'statistics'->'all_trades'->'all'->>'all_trades_perc_profit')::integer as profitable_perc
FROM trade_statistics
) sq1
WHERE profitable_perc > 1
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2804 次 |
| 最近记录: |