gpt4 book ai didi

mysql - 有没有办法在不将其包装在另一个选择中的情况下按计算值进行过滤?

转载 作者:行者123 更新时间:2023-11-29 02:00:10 25 4
gpt4 key购买 nike

假设我有一个像

这样的简单查询
SELECT ID, Name, SUM(qty * price) as Value
FROM Docs
WHERE Name like '%something%' OR Value like '%something%'
GROUP BY ID, Name

Docs 表对每一个离开库存的产品都有一条记录:

ID: the id of the document through which the product was released from the inventory (for example an invoice or a delivery note)  
Name: the name of the document
qty: the number of units that were released
price: the unit price of the product in said inventory

(实际的表格比那个复杂得多,但为了清楚起见我简化了很多)

如您所见,我发布的查询是一揽子搜索。我想列出所有文档及其 ID、名称和总值,其名称或值类似于某些用户输入。但是我实际上不能在 WHERE 子句中使用 Value 列。

我可以将它包装在另一个 select * FROM () 中并对其进行搜索。但是我必须处理的查询要复杂得多,以这种方式更改它会带来很多麻烦。

如果我想进行这种搜索,有什么方法可以避免包装整个查询吗?

最佳答案

HAVING 会做同样的事情吗?

SELECT ID, Name, SUM(qty * price) as Value
FROM Docs
HAVING Value like '%something%' OR Name like '%something%'

关于mysql - 有没有办法在不将其包装在另一个选择中的情况下按计算值进行过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16835533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com