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 01-17-2006, 01:01 PM
dodgechargerfan dodgechargerfan is offline
 
Join Date: Aug 2005
Posts: 94
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Reducing queries to the DB

I am relatively new to this stuff and I'm trying to wrap my head around a concept before I try a little bit of coding on my board.

SELECT field1 from prefix_table where field5 = 1;
SELECT field2 from prefix_table where field5 = 1;

Since I am trying to match on the same field, I figure there must be a way to get both pieces of data with one query.

I think I need to use an array, but can I just grab the fields I'm intersted in, or is it easier to move all of the data into an array and then just reference the fields with array[field1], array[field2].... etc...?
Reply With Quote
  #2  
Old 01-17-2006, 01:39 PM
Brad Brad is offline
 
Join Date: Nov 2001
Posts: 4,765
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If you're working with the same table you can get them like this:

[sql]SELECT field1, field2 FROM table[/sql]

You can also use a * if you want to grab everything from the table at once:

[sql]SELECT * FROM table[/sql]

If you're working with two or more tables you can use a join to get fields from multiple tables in one query. For example:

[sql]SELECT user.username AS username, role.roleid AS roleid FROM user LEFT JOIN role AS role (user.usergroupid = role.usergroupid) WHERE user.posts > 5 GROUP BY user.usergroupid ORDER BY username ASC[/sql]

The first thing you'll notice is the field names have changed. When working with multiple tables it's important to define which table you are trying to get information from, thus you should always prefix the field name by adding the table name infront of it: tablename.fieldname. To make this a little easier you can use the AS command to map a new name to a field, in the above example user.username would simply become username.

The next important thing is our join, in this example I've used a LEFT JOIN.

[sql]LEFT JOIN role AS role (user.usergroupid = role.usergroupid)[/sql]

role is the table name we will be joining, the bit of text inside the ( - ) tells mySQL to look for matches based on common data in both tables. In this example the common data is the usergroupid but it can be anything. You can also do multiple joins in one query if you need to work with more than two tables at the same time.
Reply With Quote
  #3  
Old 01-17-2006, 02:41 PM
dodgechargerfan dodgechargerfan is offline
 
Join Date: Aug 2005
Posts: 94
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks. That helps a lot.

From what I see, then, if I want to process all of the results from the query, I'd be best off to build a product plug-in with a function that will roll through all of the returned records using a while() function. I think I can piece that together from other coders' examples.
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 06:29 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.03298 seconds
  • Memory Usage 2,173KB
  • Queries Executed 13 (?)
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)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (3)post_thanks_box
  • (3)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (3)post_thanks_postbit_info
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)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_postinfo_query
  • fetch_postinfo
  • 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