按 GAE 数据存储中的计算列排序

发布于 2025-01-03 23:44:02 字数 585 浏览 1 评论 0原文

如何对按计算列排序的结果进行查询? (我无法预先计算其他列中的结果来进行排序!)

示例:

在这样的 RDBMS 表中:

TABLE products(
    product_id  INTEGER      NOT NULL,
    description VARCHAR(255) NOT NULL,
    price       DECIMAL(5,2) NOT NULL,
    qty         INTEGER      NOT NULL,
);

我可以执行此查询:

SELECT   product_id, (price * qty * {{current_tax}}) AS custom_calc
FROM     products
ORDER BY custom_calc DESC;

我的问题是我有一个变量,在本例中{{current_tax}}。这样我就无法预先计算结果来进行排序,因为变量可以随时更改。

对于这样的问题有一个优雅的解决方案吗?

坦克!

How can i make a query with results sorted by a calculated column? (I can not pre calculate the result in other column to do the sorting!)

Example:

In a RDBMS tables like that:

TABLE products(
    product_id  INTEGER      NOT NULL,
    description VARCHAR(255) NOT NULL,
    price       DECIMAL(5,2) NOT NULL,
    qty         INTEGER      NOT NULL,
);

I can perform this query:

SELECT   product_id, (price * qty * {{current_tax}}) AS custom_calc
FROM     products
ORDER BY custom_calc DESC;

Im my problem i have a variable, in this case {{current_tax}}. In this way I can not pre calculate the result to do the sorting because the variable can change at any time.

there is an elegant solution to problems like this?

Tanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2025-01-10 23:44:02

正如您在文档中看到的,GQL 不支持任何SELECT 语句中的算术运算。

GQL 的存在是为了方便具有 SQL 背景的人,但低级数据存储 API 更像是索引的 k/v 存储。

如果 current_tax 始终是应用于 price * qty 的正因子,则无论如何都不会修改排序,因此我建议存储 price * qty作为新的 Product 字段 total_price 并为其创建 DESC 索引。

As you can see in the documentation, GQL does not support any arithmetic in the SELECT statement.

GQL is there as a convenience for people coming from SQL background, but the low level datastore API is much more like an indexed k/v store.

If current_tax is always a positive factor applied on price * qty it will not to modify the ordering anyway, so I would recommend storing price * qty as a new Product field, total_price and create a DESC index for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文