Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 03-22-2005, 02:42 PM
sully02 sully02 is offline
 
Join Date: Jul 2004
Posts: 161
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Retrieve values from 2 tables with minimal queries

I am trying to figure out how do a query where I would use one of thevalues from one query to access a value in another table with as littlequeries as possible. Just to make things simple, I'll use actualtable/field names.

I plan to run a query that fetches all values from the table'lmd_draftpicks' The main variable is pickid, which is the number ofthe draft pick involved. In that table I've got a variable namedteamid, which is the # correlating to an NFL team.

The first way that popped into my head is to assign the value of theteamid to a random variable such as x, then run another query to fetchthe name of the team where teamid = x.

That results in 2 queries per draft pick. Is there a way to do this with 1 query per pick, or so less overall queries are used?

Finally, to see if I'm doing this right, once I have the team nameselected, could I then replace the teamid variable in the first querywith the teamname?

(I'm sure some of this is simple to you guys, but I am still trying to learn PHP/MySQL).

Thanks in advance.
Reply With Quote
  #2  
Old 03-22-2005, 03:24 PM
sabret00the's Avatar
sabret00the sabret00the is offline
 
Join Date: Jan 2003
Location: London
Posts: 5,268
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

can't you just use a left join?
Reply With Quote
  #3  
Old 03-22-2005, 03:30 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
"SELECT draftpicks.*, teams.*
FROM " 
TABLE_PREFIX "draftpicks AS draftpicks
LEFT JOIN " 
TABLE_PREFIX "teams AS teams ON (teams.teamid = draftpicks.teamid)
WHERE draftpicks.pickid = 
$selecteddraft
Something like that?
Reply With Quote
  #4  
Old 03-22-2005, 03:44 PM
sully02 sully02 is offline
 
Join Date: Jul 2004
Posts: 161
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

So left join basically allows you to append a value to the query from another table using the value from the initial query? Do I understand this right?
Reply With Quote
  #5  
Old 03-22-2005, 03:57 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It lets you join (read from 2 tables which share something in common) the columns from 2 tables.
Reply With Quote
  #6  
Old 03-22-2005, 04:11 PM
sully02 sully02 is offline
 
Join Date: Jul 2004
Posts: 161
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK, so if I understand this right, I'd use a query like this:

Code:
SELECT *
FROM lmd_draftpicks AS draftpicks
LEFT JOIN lmd_teams AS teams ON (teams.teamid = draftpicks.teamid)
WHERE draftpicks.pickid = $selecteddraft
LEFT JOIN lmd_players AS players on (players.playerid = draftpicks.playerid)
WHERE draftpicks.playerid = $selected draft
Notes:
1 - I want to select all values from the draftpicks table
2 - The tables aren't the same prefix as my vBulletin tables, thus I don't plan to use the TABLE_PREFIX option
3 - Eventually I will be adding another table that has a list of prospects, so I'd need to use LEFT JOIN for 2 separate tables
4 - I will use the teamid field to look up the teamname field and put that in on my template
5 - I will use the playerid field to look up the playername and school fields to put those on my template

Did I do this right?
Reply With Quote
  #7  
Old 03-22-2005, 04:45 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by sully02
1 - I want to select all values from the draftpicks table
4 - I will use the teamid field to look up the teamname field and put that in on my template
5 - I will use the playerid field to look up the playername and school fields to put those on my template
So you don't want to select only the columns from the draftpicks table, but also want the playername and teamname.

The WHERE clause you only add once on the end.

I added a $selectedpick field as an example of how to select all info for just 1 draft. This will first (with the WHERE clause) select all rows from your primary table (draftpicks) with pickid = $selectedpick, then it will find all corresponding rows from both the teams and the players tables, based on the selection following the "ON" in their respective LEFT JOIN clauses.

You query would look like this:
[sql]SELECT draftpicks.*, teams.teamname, players.playername
FROM lmd_draftpicks AS draftpicks
LEFT JOIN lmd_teams AS teams ON (teams.teamid = draftpicks.teamid)
LEFT JOIN lmd_players AS players on (players.playerid = draftpicks.playerid)
WHERE draftpicks.pickid = $selectedpick[/sql]
Reply With Quote
  #8  
Old 03-22-2005, 05:22 PM
sully02 sully02 is offline
 
Join Date: Jul 2004
Posts: 161
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I think that should work out good. The final result will look something like this:

http://sports-boards.net/forums/2004.php

This file (From last year) is saved as one giant HTML template.

For this year, I'm putting it in the database. The teamid wouldreference the team name and post it on there, then the playerid wouldreference the player's position, name, and school.

As I examine this further, I dont know how complex I want to makeit,but I suppose it wouldn't hurt to have a table for positions andschools, or would that make the query too large. I want to make it easyto get the information, but at the same time, I don't want a querythat'll be a page long, you know?
Reply With Quote
  #9  
Old 03-23-2005, 11:02 PM
sully02 sully02 is offline
 
Join Date: Jul 2004
Posts: 161
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Now that I've made a little progress on it, I can give more insight to perfect the query I have:

Here is the list of prospects I have. They're sorted in ascending order by playerid.

http://sports-boards.net/draft/prospects.php?styleid=7

For this query, I want to use the given playerid tofindtheplayer'sname,position, and school (playername,position,schoolfieldsrespectively) andappend them to the array. Here'sthecurrentquery Ihave:

Code:
$picks=$DB_site->query("
	SELECT draftpicks.*, teams.teamname, players.*
	FROM lmd_draftpicks AS draftpicks
	LEFT JOIN lmd_teams AS teams ON (teams.teamid =	draftpicks.teamid)
	LEFT JOIN lmd_players AS players ON (players.playerid = draftpicks.playerid)
	WHERE draftpicks.pickid = $selectedpick
	ORDER BY pickid ASC");
Is that right? And finally, when I get to the template,howwouldIaccessthe player information? The variable I have to fetchthedatais$info,but the variable given to me in the querybyMarcois$selectedpick. Dothose variables have to be the same name?

I'm going to assume that since there are no conflictingfieldnames,Icanput the default field names in thetemplate($info[school],forexample).

*Crosses fingers, hoping he's doing this right*

(On a side note, I have to give a thumbs down to the"Automergeddoublepost" feature. I'd rather it show up as a new replybecauseotherwise the post gets ignored, and it is messing up thespacing onthe already existing post, but I digress...)
Reply With Quote
  #10  
Old 03-25-2005, 01:02 AM
sully02 sully02 is offline
 
Join Date: Jul 2004
Posts: 161
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by sully02
Now that I've made a little progress on it, I can give more insight to perfect the query I have:

Here is the list of prospects I have. They're sorted in ascending order by playerid.

http://sports-boards.net/draft/prospects.php?styleid=7

For this query, I want to use the given playerid tofindtheplayer'sname,position, and school (playername,position,schoolfieldsrespectively) andappend them to the array. Here'sthecurrentquery Ihave:

Code:
$picks=$DB_site->query("
	SELECT draftpicks.*, teams.teamname, players.*
	FROM lmd_draftpicks AS draftpicks
	LEFT JOIN lmd_teams AS teams ON (teams.teamid =	draftpicks.teamid)
	LEFT JOIN lmd_players AS players ON (players.playerid = draftpicks.playerid)
	WHERE draftpicks.pickid = $selectedpick
	ORDER BY pickid ASC");
Is that right? And finally, when I get to the template,howwouldIaccessthe player information? The variable I have to fetchthedatais$info,but the variable given to me in the querybyMarcois$selectedpick. Dothose variables have to be the same name?

I'm going to assume that since there are no conflictingfieldnames,Icanput the default field names in thetemplate($info[school],forexample).

*Crosses fingers, hoping he's doing this right*

(On a side note, I have to give a thumbs down to the"Automergeddoublepost" feature. I'd rather it show up as a new replybecauseotherwise the post gets ignored, and it is messing up thespacing onthe already existing post, but I digress...)
A little update: I got the latter query to work without the where, which allows for the displaying of the entire draft:

http://sports-boards.net/draft/draft.php

Now I need to make it so you can view an individual round (draft.php?r=1, for example)

Would the WHERE statement at the end then be "WHERE draftpicks.roundid = r.roundid" ?
Reply With Quote
Reply

Thread Tools
Display Modes

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 04:40 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04937 seconds
  • Memory Usage 2,261KB
  • 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
  • (3)bbcode_code
  • (1)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete