View Single Post

   
  #2 (permalink)  
Old 04-24-2008, 06:09 PM
Captain Paralytic
 
Posts: n/a
Default Re: How to not repeat functions?

On 23 Apr, 06:49, incuso <numismat...@gmail.com> wrote:
> I am extensively using string functions on a table fields to extract
> suitable data.
>
> Unfortunately, I do not understand how to avoid to repeat them (that I
> assume is wasting CPU cycles).
>
> Just an example
>
> SELECT * FROM a bunch of tables WHERE other conditions AND
> SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".")," g",""),"-",1) BETWEEN
> X AND Y OR SUBSTRING_INDEX(REPLACE(REPLACE(peso,",","."),"
> g",""),"-",1) BETWEEN X AND Y OR
> (SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".")," g",""),"-",1)<X
> AND SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".")," g",""),"-",1)>Y)
>
> Ok, ok there is an error in the function usage, but this is not the
> point, it is only an example.
>
> You can see that "SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".") ,"
> g",""),"-",1)" is reapeted 4 times, is there any way to use it only
> once?
>
> Thanks!
> M.


Put it in the SELECT part, alias it and use the alias in a HAVING
clause
Reply With Quote