View Full Version : grouping by for a query
sabret00the
02-08-2005, 11:23 AM
is their a method at all to get it only select XX records from each group?
sabret00the
02-09-2005, 12:15 PM
/me thinks w00t
/me bumps the thread
Andreas
02-09-2005, 12:32 PM
I don't understand what you actually want to retrieve.
Can you give some example data and what you want to have returned?
Marco van Herwaarden
02-09-2005, 12:43 PM
What he wants is to retrieve all "groups" from a group table together with maximum 10 detailrows from a groupitems table for every group in 1 query.
Just a simple left join, except he wants to limit the number of rows from the second table.
I don't know of any solution coded in 1 SQL (or to lazy to think about it :D ).
Only a loop through the groups and then fetching max 10 items i can think of.
sabret00the
02-09-2005, 01:50 PM
yeah my initial idea was gonna be a query per group but if you have 12 catergories that's like an additional 12 queries per page, not good.
I don't understand what you actually want to retrieve.
Can you give some example data and what you want to have returned?
an example would be (to elablorate on marco) i have
a table (catergories)
catid title description
1 Automotive For groups referring to cars, bikes or such
2 Computers For groups referring to computers, whether hardwar...
3 Education For groups regarding learning, whether it be help ...
4 Entertainment For groups surrounding anything entertaining, visu...
5 Games For groups surrounding games you love, digital or ...
6 Health & Fitness For groups pertaining to well-being
7 Home & Families For groups regarding the home life
8 Lifestyles For groups surrounding lifestyles whether it be go...
9 Places & Travel For groups referring to towns, cities and holidays
10 Religion & Belief For groups surrounding christianity, islam, buddis...
11 Sports & Recreation For groups referring to sports, whether it be Foot...
12 Others For everything else
now these are leftjoined to a groups table so it will display all groups that fall into each catergory, however i went to make it so it shows a maximum of ten records for each catid
here's the code that pulls this data from the database
$grps_categories = $DB_site->query("
SELECT grps.groupid, grps.title, grps.description, grps.catid, grps_catergories.title AS catname, grps.approved, grps.leaderid
FROM grps
LEFT JOIN grps_catergories ON (grps_catergories.catid = grps.catid)
WHERE grps.approved=1
ORDER BY grps_catergories.catid ASC, grps.title ASC
");
if (!$DB_site->num_rows($grps_categories))
{ // no groups, this becomes obselete after the first group gets created
eval("\$grps_catbits = \"" . fetch_template("groups_browse_cathead") . "\";");
$grps_catbits .= "<tr><td class=\"alt1\" align=\"center\">no groups found in this catergory at this time</td></tr>";
eval("\$grps_catbits .= \"" . fetch_template("groups_browse_catfoot") . "\";");
}
else
{ // display the information
$row = "0";
while ($grps_categoryinfo = $DB_site->fetch_array($grps_categories))
{
if ($row == 0)
{
eval("\$grps_catbits .= \"" . fetch_template("groups_browse_cathead") . "\";");
// reset catname store
$grps_catidstore = $grps_categoryinfo['catid'];
$seperator = TRUE;
}
if ($grps_catidstore != $grps_categoryinfo[catid] AND $row != 0)
{
eval("\$grps_catbits .= \"" . fetch_template("groups_browse_catfoot") . "\";");
eval("\$grps_catbits .= \"" . fetch_template("groups_browse_cathead") . "\";");
}
$grps_categoryinfo['title'] = stripslashes($grps_categoryinfo['title']);
$grps_categoryinfo['description'] = nl2br(stripslashes($grps_categoryinfo['description']));
$grps_categoryinfo['catname'] = stripslashes($grps_categoryinfo['catname']);
eval("\$grps_catbits .= \"" . fetch_template("groups_browse_catbit") . "\";");
if ($grps_categoryinfo[catid] != $grps_catidstore)
{
// reset catname store
$grps_catidstore = $grps_categoryinfo['catid'];
}
$row++;
} //now echo the date into the template
eval("\$grps_catbits .= \"" . fetch_template("groups_browse_catfoot") . "\";");
}
thinking about it, couldn't i add a counter for the catergory and then if it reaches 10 then not echo the template till the catid changes? or would that be intensive on the server resources?
Dean C
02-09-2005, 04:54 PM
Hmm, this is outside of my SQL knowledge but I have a feeling it can be done. Post your question up in the MySQL forums on sitepoint and rudy will help you out ;) Just your question and db structure though, he's not a PHP programmer.
sabret00the
02-10-2005, 12:28 PM
thanks dean that worked out perfectly.
here's the code incase anyones interested
SELECT grps.groupid, grps.title, grps.description, grps.catid, grps_catergories.title AS catname, grps.approved, grps.leaderid
FROM grps_catergories
INNER JOIN grps ON grps_catergories.catid = grps.catid AND grps.approved = 1
INNER JOIN grps AS G2 ON grps_catergories.catid = G2.catid AND G2.approved = 1 AND grps.title >= G2.title
GROUP BY grps_catergories.title, grps.title, grps.description, grps.leaderid
HAVING COUNT(*) <= 10
ORDER BY grps.title
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.