0

I'm attempting to use MySQL's user defined variables, but due to restrictions I can't use them in the same way as it demonstrates, where one query sets the variable and a second uses it. My requirement is to be able to use it multiple times in a query.

Consider the following:

SELECT (Some Really Taxing Calculation) AS Total FROM Purchases
WHERE Total < 55 AND itemName = "Bananas"
OR Total > 90 AND itemName = "Apples"
OR Total = 30 AND itemName = "Peaches"
ORDER BY Total

It'd be really nice to only run the Total calculation once, and then use the result multiple times throughout the query. Right now, the only way I've gotten it to work in one query is to rerun it for every place it's used as Total returns an error saying an unknown column has been used.

5
  • Can you describe the restrictions? Commented Jun 22, 2011 at 22:39
  • If you put it in the FROM clause, wouldn't it only be calculated once, and you can get the value through something like total.Value. Commented Jun 22, 2011 at 22:40
  • @Ryan Gross, single queries, I can't run multiple queries that relate to each other. Commented Jun 22, 2011 at 22:42
  • @Dirk: I'm unfamiliar with the approach you're talking about, if you think it would work and can explain I'll give it a whirl. Commented Jun 22, 2011 at 22:42
  • The "taxing calculation", is it returning a (int/float) value or is it somehow returning column names? In the first case I cant see how you are using the value and compare it with another value. Commented Jun 22, 2011 at 23:17

2 Answers 2

1

Imbed the total calculation in a sub-query...

SELECT
  Total
FROM
  (SELECT (Some Really Taxing Calculation) AS Total, * FROM Purchases) AS Purchases
WHERE Total < 55 AND itemName = "Bananas"
   OR Total > 90 AND itemName = "Apples"
   OR Total = 30 AND itemName = "Peaches"
ORDER BY
  Total

Note: The sub-query (inline-view) is expanded by MySQL when generating it's PLAN. Putting brackets around the query doesn't necessarily mean you're forcing it to be stupidly inefficient :)

Sign up to request clarification or add additional context in comments.

Comments

0
SELECT *, total.Value
FROM Purchases, (SELECT COUNT(*) as Value .... Some Really Taxing Calculation) total
WHERE Total < 55 AND itemName = "Bananas"
OR Total > 90 AND itemName = "Apples"
OR Total = 30 AND itemName = "Peaches"
ORDER BY Total

So basically, do your calculation in a nested SELECT type query in the FROM clause and give it a name of total than you can then use else where in your query.

2 Comments

Okay, so if it's using a calculation based on rows, I have to recreate that environment within the Total, so basically apply my other where's and joins twice?
If you have to in order to get the 'Some Really Taxing Calculation' value, then yes, you'd have to reapply the same wheres a joins. Write your calculation query/statement first (with whatever dependencies it needs), then wrap it with parens and stick it into the FROM clause of another SQL query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.