tags:

views:

60

answers:

2

I have a scenario where I'm calculating something in the WHERE clause of my SQL, but I also want to get that calculation - since it's expensive. Is it possible to get the results of something done in the WHERE clause, like this:

SELECT `foo` FROM `table` WHERE (foo = LongCalculation(`column`))

Wishful thinking, or possible with MySQL?

EDIT: Calculation is column dependent

+3  A: 
set @bar = LongCalculation();
select foo from table where foo=@bar;
cherouvim
+1: assuming `LongCalculation` is not row-dependent (does not take any columns from the `table`.
van
A: 

A bit of re-working @cherouvim's idea and I got it to work with row-dependent functions:

set @bar = 0;

SELECT
  `product_name`,
  @bar AS `stock`
FROM `jos_vm_product`
WHERE (@bar := `product_in_stock`) > 0 
Kristopher Ives