Question : How to get a one-to-many result set in mysql

Hi,

Let's say I have a database with 2 tables, foodtypes and foodnames (show below). Foodtypes has 3 different types of food and foodnames has examples of things that belong in that type. I use these tables to post on a webpage.  If you select the type of food in a form, then you get back the different foods in the type that you selected.

foodtypes
-----------------
|ID | Name      |
-----------------
|1  |Meat       |
|2  |Fruit      |
|3  |Vegetable  |
-----------------

foodnames
-----------------------------
|ID | Type      | Name          |
-----------------------------
|1  |1             |Pork          |
|2  |1            |Beef          |
|3  |2          |Apple          |
|4  |3            |Broccoli   |
|5  |2            |Pear          |
|6  |2            |Strawberry |
|7  |3            |Peas          |
|8  |1            |Chicken    |
|9  |1            |Goat          |
|10 |3            |Celery     |
-----------------------------

If I execute the query:

Select a.Name, b.Name, b.ID from foodtypes a, foodnames b where a.ID=b.Type and a.ID in (2,3);

the expected outcome would be:

-------------------------------
| a.Name    |b.Name     |b.ID |
-------------------------------
|Fruit      |Apple      |3    |
|Fruit      |Pear      |5    |
|Fruit      |Strawberry |6    |
|Vegetable  |Broccoli      |4    |
|Vegetable  |Celery      |10   |
|Vegetable  |Peas       |7    |
-------------------------------

Is there a way to design the query so that I dont get repeated values of a.Name, the type of food. I want to be able to refine it so that the result would be similar except Fruit and Vegetable only appear once.  For example:

----------------------------------
| a.Name    |b.Name       |b.ID |
-----------------------------------
|Fruit           |Apple            |3    |
|                  |Pear            |5    |
|                  |Strawberry |6    |
|Vegetable  |Broccoli      |4    |
|                   |Celery             |10   |
|                   |Peas           |7    |
-----------------------------------

Is this even possible using mysql? Would I need to use php? Is there a way I can call it using php and then manipulate afterwards so that it prints the way I want it to?  Does what I'm asking for make sense?

Answer : How to get a one-to-many result set in mysql

You could ask another question to get a quick(er) response (?)  :)
Try this

       $dupDetect = "";
       $dup2Detect = "";
        while($row = mysql_fetch_row($query)) {
                echo "<tr class='tab_bg_1'>";
                $colindex = 1;
                $col2index = 1;
                foreach ($row as $str) {
                        if ($colindex==1) {  // change this to the column index desired
                             if ($str == $dupDetect ) {
                               $str=''; // blank if it is the same as previous
                             }
                             else {
                               $dupDetect = $str; // store it to compare against next
                               $dup2Detect = ""; // reset dup2 at the same time
                             }
                        } else if ($colindex==2) {
                             if ($str == $dup2Detect ) {
                               $str=''; // blank if it is the same as previous
                             }
                             else {
                               $dup2Detect = $str; // store it to compare against next
                             }
                        }
                        echo "<td>".$str."</td>";
                        $colindex++;
                }
                echo "</tr>";
        }
Random Solutions  
 
programming4us programming4us