Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 02-08-2005, 11:23 AM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default grouping by for a query

is their a method at all to get it only select XX records from each group?
Reply With Quote
  #2  
Old 02-09-2005, 12:15 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

/me thinks w00t

/me bumps the thread
Reply With Quote
  #3  
Old 02-09-2005, 12:32 PM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't understand what you actually want to retrieve.

Can you give some example data and what you want to have returned?
Reply With Quote
  #4  
Old 02-09-2005, 12:43 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 ).

Only a loop through the groups and then fetching max 10 items i can think of.
Reply With Quote
  #5  
Old 02-09-2005, 01:50 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #6  
Old 02-09-2005, 04:54 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 02-10-2005, 12:28 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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]
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:38 PM.


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.03836 seconds
  • Memory Usage 2,249KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete