Sqlite: Second Select Inside Loop

Is it possible to have a second select loop inside a and already looping select?

Like this:

for row in db:nrows("SELECT id From tablename") do for innerRow in db:nrows("Select code From tablename2 where id = "..row.id) do print(innerRow.code) end end

The thing is that i cant do a join since I only want to return one row per “id”, but many rows with “code”. I dont get it to work with the code above, the inner select doesnt run. What am I doing wrong?