Jump to content

Help with query/output for one to many.


sledge4

Recommended Posts

I have two tables, for which I want to output data from.  Here's an example of what I am trying to accomplish.  The ID's link/join each table.

 

Table 1 (ID | Name)

1 | John

2 | Bob

3 | Sara

 

Table 2 (ID | Type)

1 | Soda

1 | Candy

2 | Beer

2 | Ice Cream

3 | Popcorn

 

I'm trying to output the data like this:

 

John - Soda, Candy

Bob - Beer, Ice Cream

Sara - Popcorn

 

Instead I get this

 

John - Soda

John - Soda

Bob - Beer

Bob - Ice Cream

Sara - Popcorn

 

Any point in the right direction would be GREATLY appreciated.

 

Thanks!

Link to comment
Share on other sites

Ok, group_concat worked...thanks!  But I have another question.  Is it possible to link each result?

 

Here's my code:

 

<?
$query = mysql_query("SELECT a_site_residents.name AS Resident, GROUP_CONCAT(a_site_stables.stable ORDER BY a_site_stables.stable ASC SEPARATOR ', ') AS Stable FROM a_site_residents, a_site_stables WHERE a_site_residents.id=a_site_stables.id GROUP BY a_site_residents.name");
$numrows=@mysql_num_rows($query);
if($numrows != 0) {
while ($result = mysql_fetch_array($query)) {
?>

<table border="1" width="100%">
<tr>
	<td width="100" valign="top"><? echo $result['Resident']; ?></td>
	<td><? echo $result['Stable']; ?></td>
</tr>
</table>

<? } } ?>

 

 

What I'm wondering if I can do is link each "Stable" result that is being output.

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.