zechdc
zechdc

Reputation: 3404

Can you do a For Each Row loop using 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?

Upvotes: 27

Views: 118180

Answers (3)

Kudapucat
Kudapucat

Reputation: 138

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

Upvotes: 5

panupan
panupan

Reputation: 1242

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

Upvotes: 19

Zohaib
Zohaib

Reputation: 7116

In the link you provided, thats not a loop in sql...

thats a loop in programming language

they are first getting list of all distinct districts, and then for each district executing query again.

Upvotes: 2

Related Questions