Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 06-11-2008, 01:51 AM
ZomgStuff ZomgStuff is offline
 
Join Date: Feb 2007
Posts: 469
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Need Help with double LEFT JOIN SQL Query

So currently this is my query.

PHP Code:
                $rating_load $db->query_read("
                SELECT  p.postid, p.threadid, p.title, p.pagetext, r.*
                FROM " 
TABLE_PREFIX "erate AS r
                LEFT JOIN "
.TABLE_PREFIX."post AS p ON(p.postid = r.pid)
                WHERE r.rating = '"
.$rate_id."' AND r.uid = '".$user_id."' AND p.userid = '".$thisUser."'
                ORDER BY p.postid DESC
                "
); 
Only problem is that I actually need thread.title as opposed to post.title, but I would sitll like to keep post.pagetext.

Now how do I do a double LEFT JOIN to link all there of these into a single array, I need to be able to use the following variables to output them into HTML.

Quote:
thread.title, thread.threadid
post.pagetext, post.postid
I've made a little graphic representation of the 3 tables that I have.


Thank you in advance.

Edit: This has been resolved, thanks!
Reply With Quote
  #2  
Old 06-11-2008, 01:55 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What is it exactly you are wanting to pull with the query? And why not just LEFT JOIN the thread table?
Reply With Quote
  #3  
Old 06-11-2008, 02:06 AM
ZomgStuff ZomgStuff is offline
 
Join Date: Feb 2007
Posts: 469
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Because I still need the post.pagetext (which is the actual post content).

Basically I need to do a search by a "rating" type and "userid" , and be able to get the thread id, post id, post content, and thread title.

Would it just be simpler to do two separate LEFT JOIN's, one linking thread and the other post?
Reply With Quote
  #4  
Old 06-11-2008, 02:30 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That's the way I would go but I'm far from an expert on queries.
Reply With Quote
  #5  
Old 06-11-2008, 02:37 AM
ZomgStuff ZomgStuff is offline
 
Join Date: Feb 2007
Posts: 469
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How would I be able to get the post conent. Would it be effcient to do two seperate LEFT JOIN's and then implode the two arrays?


--------------- Added [DATE]1213156014[/DATE] at [TIME]1213156014[/TIME] ---------------

I actually got the double LEFT JOIN query working, it was much easier that I thought.

Thanks for trying to help.

Finished product
Reply With Quote
  #6  
Old 06-11-2008, 04:23 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How does the query look now? I'm curious to see what you came up with.
Reply With Quote
  #7  
Old 06-11-2008, 10:58 AM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

[sql]
SELECT p.postid, p.threadid, p.title, p.pagetext, r.*, t.threadtitle
FROM " . TABLE_PREFIX . "erate AS r
LEFT JOIN ".TABLE_PREFIX."post AS p ON(p.postid = r.pid)
LEFT JOIN ".TABLE_PREFIX."thread AS t ON(p.threadid = t.threadid)
WHERE r.rating = '".$rate_id."' AND r.uid = '".$user_id."' AND p.userid = '".$thisUser."'
ORDER BY p.postid DESC
[/sql]
I think that should work, not tested though. Might be a more efficient way of doing it but thats the way I would have done it
Reply With Quote
  #8  
Old 06-11-2008, 01:11 PM
ZomgStuff ZomgStuff is offline
 
Join Date: Feb 2007
Posts: 469
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Boofo View Post
How does the query look now? I'm curious to see what you came up with.
[SQL] SELECT p.postid, p.threadid, p.pagetext, t.threadid, t.title, r.*
FROM " . TABLE_PREFIX . "erate AS r
LEFT JOIN ".TABLE_PREFIX."post AS p ON(p.postid = r.pid)
LEFT JOIN ".TABLE_PREFIX."thread as t ON(t.threadid = p.threadid)
WHERE r.rating = '".$rate_id."' AND r.uid = '".$user_id."' AND p.userid = '".$thisUser."'
ORDER BY p.postid DESC
[/SQL]
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 12:54 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.03846 seconds
  • Memory Usage 2,224KB
  • 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
  • (2)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