Jump to content

Can't get "GROUP BY" to work


sonicslp

Recommended Posts

I'm a bit of a newbie here, so I'll try to be thorough.

 

TABLE: ProductGroupsLineItem

Fields: ItemID_t, Group_t, SubGroup_t, Brand_t, Active_n

 

Example data:

CL2000, Braking, Control Arms, MyBrand, 1

CL3000, Braking, Braded Cables, MyBrand, 1

CL4000, Custom Gauges, Blue, MyBrand, 1

CL5000, Braking, Fluid, MyBrand, 1

 

Result I would like:

Group_t = Braking

Group_t = Custom Gauges

 

I only have "SubGroup_t" when starting. First I find all "ItemID_t" that contain the correct "SubGroup_t" value. Then, I find all "Group_t" from that result. Ultimately, I just want to find every possible group from that result, but I only want to display one of each group. What I'm getting with the query below is:

 

Group_t = Braking

Group_t = Braking

Group_t = Custom Guages

Group_t = Braking

 

I use "Group By" in other places and it works great, I assume it has something to do with the nested "while"?

 

            $r1 = mysql_query("SELECT ItemID_t FROM ProductGroupsLineItem WHERE Brand_t='$MFGNAME' AND SubGroup_t='$subgroup'");

            while ($row1 = mysql_fetch_array($r1)) {
            		$ItemID = $row1['ItemID_t'];

            		$r2 = mysql_query("SELECT Group_t FROM ProductGroupsLineItem WHERE Brand_t='$MFGNAME' AND ItemID_t='$ItemID' AND Active_n='1' GROUP BY Group_t ORDER BY Group_t ASC") or die(mysql_error());

            		while ($row2 = mysql_fetch_array($r2)) {
            			$groupGeneral = $row2['Group_t'];
            			echo $groupGeneral;
            			echo '<br />';
                }
            }

Link to comment
https://forums.phpfreaks.com/topic/194856-cant-get-group-by-to-work/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.