0

Im looking for a general solution for such queries:

SELECT * FROM A
LEFT JOIN B on A.ID = B.FK_ID
SELECT * FROM A

LEFT JOIN B on A.ID = B.FK_ID
LEFT JOIN C on B.ID = C.FK_ID

to have it in an array, so joined records would be in a nested arrays. It's very complicated isn't it? All I have is the query and fetched all records

EDIT:

it normally would return

A.col1  A.col2  A.col3  B.col1  B.col2  B.col3

if A has 4 rows and 2 records of B connects to A, A will have 5 lines. Instead, I want this:

A.col1  A.col2  A.col3  B (array)
1
  • I'm sorry but what is your question? What is a "joined record"? How do you decide which columns returned by your query are nested and to what depth? There is one column which exists in both tables. Commented May 5, 2012 at 9:52

1 Answer 1

1

When you construct the source array slightly different you can use this function.

It's a duplicate (http://stackoverflow.com/questions/8431463/more-efficient-hierarchy-system/8431551) (parent_id,id,title):

$q = mysql_query("SELECT id, parent_id, name FROM categories");
while ($r = mysql_fetch_row($q)) {
  $names[$r[0]] = $r[2];
  $children[$r[0]][] = $r[1];
 }

function render_select($root=0, $level=-1) {
  global $names, $children;
  if ($root != 0)
    echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
  foreach ($children[$root] as $child)
    render_select($child, $level+1);
}

echo '<select>';
render_select();
echo '</select>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.