vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   grouping by for a query (https://vborg.vbsupport.ru/showthread.php?t=75959)

sabret00the 02-08-2005 11:23 AM

grouping by for a query
 
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.

Quote:

Originally Posted by KirbyDE
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
PHP Code:

            $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

[sql] 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
[/sql]


All times are GMT. The time now is 02:39 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01766 seconds
  • Memory Usage 1,759KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete