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 03-28-2005, 10:13 PM
mtha's Avatar
mtha mtha is offline
 
Join Date: Jul 2002
Location: US
Posts: 775
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default [MySQL] Help Optimize queries to select data from 2 tables

I want to do the following two thing:


1- Select all posts (with its information) belong to one thread
something like:

PHP Code:
$threadid == X;

$posts $DB_site->query("
                    SELECT post.*, post.userid FROM post WHERE (post.threadid = 
$threadid)
"
);

$post $DB_site->fetch_array($posts
giving me a list of posts, says 15 posts (15 rows)


2. For each posts (each row), there is one userid, I need to select all awards that belong to that userid
something like

PHP Code:
$alluserawards =  $DB_site->query("
                    SELECT award.* FROM award WHERE userid=
$post[userid]
"
); 
giving me a list of awards, says 5 awards (5 rows)

if I run query #2 for each of the rows, it will run 15 times, + 1 query for #1,

is there any way to optimize the queries? so that I can somehow get a list of awards for each userid.
Reply With Quote
  #2  
Old 03-28-2005, 11:18 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Untested:

[sql]
SELECT post.*
awards.*
FROM " . TABLE_PREFIX . "post as post
LEFT JOIN
post
ON
post.userid = award.userid
[/sql]

You didn't give any info on your db schema for the awards table so I'm afraid I can't be of much help other than the query above which may or may not work depending what you have in the awards table
Reply With Quote
  #3  
Old 03-29-2005, 12:57 AM
mtha's Avatar
mtha mtha is offline
 
Join Date: Jul 2002
Location: US
Posts: 775
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dean C
Untested:

[sql]
SELECT post.*
awards.*
FROM " . TABLE_PREFIX . "post as post
LEFT JOIN
post
ON
post.userid = award.userid
[/sql]

You didn't give any info on your db schema for the awards table so I'm afraid I can't be of much help other than the query above which may or may not work depending what you have in the awards table
yeah, my mind was just out the query is kind of simple. Thanks Dean C
The db schema is available in my new hack
https://vborg.vbsupport.ru/showthread.php?t=78934

PHP Code:
            $userawards =  $DB_site->query("
                SELECT a.*, au.*, post.userid, post.postid 
             FROM " 
TABLE_PREFIX "post, " TABLE_PREFIX "award_user au, " TABLE_PREFIX "award a 
             WHERE 
$postids AND au.userid=post.userid AND a.award_id=au.award_id
                GROUP BY au.issue_id
            "
); 
Reply With Quote
  #4  
Old 03-29-2005, 03:17 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You really should use the LEFT JOIN like Dean showed you.

Also you will need to add an AS to the FROM clauses:
[sql]" . TABLE_PREFIX . "post AS post[/sql]
Or things will go wrong if a table prefix is used.
Reply With Quote
  #5  
Old 03-29-2005, 10:58 AM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Dean's left join won't work, as it would just select ONE award per post.

you have two possibilities:
1) put both tables into the from tag
2) cache the results of query two with one big query, and then work with the cache
Reply With Quote
  #6  
Old 03-29-2005, 03:01 PM
mtha's Avatar
mtha mtha is offline
 
Join Date: Jul 2002
Location: US
Posts: 775
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarcoH64
You really should use the LEFT JOIN like Dean showed you.

Also you will need to add an AS to the FROM clauses:
[sql]" . TABLE_PREFIX . "post AS post[/sql]
Or things will go wrong if a table prefix is used.
Got it work perfectly here
https://vborg.vbsupport.ru/showthread.php?t=78934

Thank you very much
Reply With Quote
  #7  
Old 04-02-2005, 07:57 PM
mtha's Avatar
mtha mtha is offline
 
Join Date: Jul 2002
Location: US
Posts: 775
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dean C
Untested:

[sql]
SELECT post.*
awards.*
FROM " . TABLE_PREFIX . "post as post
LEFT JOIN
post
ON
post.userid = award.userid
[/sql]
Could you tell me the different between ON and USING on JOIN command?

says

LEFT JOIN post ON (userid)
and
LEFT JOIN post USING (userid)
Reply With Quote
  #8  
Old 04-03-2005, 03:26 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

using just allows a column-name, ON instead allows more complex conditions like for example:

[sql]SELECT * FROM post LEFT JOIN user ON (user.userid = post.userid * 2)[/sql]
or whatever (not that the statement above would be usefull )
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 10:35 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.03795 seconds
  • Memory Usage 2,241KB
  • 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_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)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