假设我有一个简单的查询,例如
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 ()
并对其进行搜索。但是我必须处理的查询要复杂得多,以这种方式更改它会很麻烦。
如果我想做这种搜索,有什么方法可以避免包装整个查询?