0

What I want to do is iterate through a set of row results (field of interest 'x') and do something with it, all in mysql.

So, fetching the rows and looping. Something like (disregard the syntax please):

BEGIN (procedure)
SET
    @need = SELECT (...)
LOOP @inside_row in @need
    ANOTHER QUERY WHERE SOMETHING = @inside_row.x
END LOOP (when @need is all iterated)
END

Does anyone know if this is doable and how? Thank you very much...

Fane
  • 1,785
  • 4
  • 23
  • 53

1 Answers1

0

The 'iteration through rows' is actually implicit when you work in sql. if you do select a+b from table, it will iterate through all rows and return a+b for each.

For your purposes you can probably use something like

select a+(select other_thing from other_table)
from table

you can even use conditions between the two queries like

select month, 
category, 
sum(sales), 
sum(sales)/(select month, sum(sales) from table as innertable where innertable.month=outertable.month group by 1) as category_percentage_of_monthly
from table as outertable
group by 1,2
AdrianBR
  • 2,670
  • 1
  • 12
  • 27
  • so you are trying to truncate tables by selecting the table names from other tables? if yes, see http://stackoverflow.com/questions/1575419/how-can-i-truncate-multiple-tables-in-mysql – AdrianBR Aug 07 '15 at 08:47