比方說,我有一個像有沒有辦法通過一個計算值,而不在另一個選擇包裝它來過濾?
SELECT ID, Name, SUM(qty * price) as Value
FROM Docs
WHERE Name like '%something%' OR Value like '%something%'
GROUP BY ID, Name
表格文檔包括每個產品的記錄是外出庫存作爲這樣的一個簡單的查詢:
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()
並做了搜索。但是我必須處理的查詢要複雜得多,並且以這種方式改變它會是一個很大的麻煩。
有什麼辦法避免纏繞整個查詢,如果我想要做這種搜索的?
你明白這個名字的過濾會影響總和的價值? –
是啊,我得更改查詢了一下,我的忘了該組。 – Bogdan