Kristopher Ives
Kristopher Ives

Reputation: 6025

Is it possible to capture data from a WHERE clause?

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

Upvotes: 2

Views: 97

Answers (2)

Kristopher Ives
Kristopher Ives

Reputation: 6025

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 

Upvotes: 1

cherouvim
cherouvim

Reputation: 31903

set @bar = LongCalculation();
select foo from table where foo=@bar;

Upvotes: 3

Related Questions