Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you do a For Each Row loop using MySQL?

Tags:

mysql

My question is related to the answer found in this post by user Consultuning on Oct 22 '09 at 18:31: mysql query to dynamically convert row data to columns

It appears that Consultuning's answer contains some MySQL with a For Each row loop.

Can you do a For Each Row loop using MySQL?

If so, could someone give me a link to the MySQL For Each Row documentation?

like image 832
zechdc Avatar asked Nov 03 '11 04:11

zechdc


People also ask

Is there a foreach in MySQL?

foreach() acts on server side only, and does not require shell access nor the mysql command line client, although it may be spawned from within the mysql client. foreach() accepts several types of collections. They are automatically recognized by their pattern.

How do I ITERATE in MySQL?

You can use these statements in the stored programs (procedures), and RETURN in stored functions. You can use one Flow Control Statement with in another. The ITERATE statement is used to restart the LOOP, REPEAT or, WHILE statements. It cannot be used outside these statements.

Can we write loop in MySQL?

The MySQL LOOP statement could be used to run a block of code or set of statements, again and again, depends on the condition. labelname : It is an optional label at the start and end. statements : They could have one or multiple statements, each ended by a semicolon (;) and executed by LOOP.

How many types of loops are used in MySQL?

The MySQL stored program language offers three types of loops : Simple loops using the LOOP and END LOOP clauses. Loops that continue while a condition is true, using the WHILE and END WHILE clauses. Loops that continue until a condition is true, using the REPEAT and UNTIL clauses.


2 Answers

The closest thing to "for each" is probably MySQL Procedure using Cursor and LOOP.

like image 117
panupan Avatar answered Nov 12 '22 23:11

panupan


Not a for each exactly, but you can do nested SQL

SELECT 
    distinct a.ID, 
    a.col2, 
    (SELECT 
        SUM(b.size) 
    FROM 
        tableb b 
    WHERE 
        b.id = a.col3)
FROM
    tablea a
like image 44
Kudapucat Avatar answered Nov 12 '22 23:11

Kudapucat