vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Can't get MySQL JOINs to work right (https://vborg.vbsupport.ru/showthread.php?t=137015)

thincom2000 01-20-2007 08:42 AM

Can't get MySQL JOINs to work right
 
I am trying to write a single query that does the following:

- reads all threads where forumid = $forumid
- reads all posts where threads.firstpostid = postid
- reads all polls where threads.pollid = pollid, but only if a poll exists

I tried:

PHP Code:

SELECT thread.*, post.*, IF(thread.pollid poll.pollidpoll.*, '')
FROM " . TABLE_PREFIX . "thread" . TABLE_PREFIX . "post" . TABLE_PREFIX . "poll"
WHERE thread.forumid = '" 
$forumid "'
    AND post.postid = thread.firstpostid
    AND IF(thread.pollid = poll.pollid, poll.pollid = thread.pollid, '1 > 0')
ORDER BY dateline DESC 

but it just gives T_STRING errors and returns NULL...

Marco van Herwaarden 01-20-2007 08:58 AM

Use LEFT JOIN's.

thincom2000 01-20-2007 04:13 PM

I don't think this has the same meaning as what I suggested in my first post, but here it is with LEFT JOINs and still not working (database error):

Code:

SELECT thread.*, post.*, IF(thread.pollid = poll.pollid, 'poll.*', '')
FROM " . TABLE_PREFIX . "thread AS thread
LEFT JOIN " . TABLE_PREFIX . "post AS post ON (post.postid = thread.firstpostid)
IF(thread.pollid = poll.pollid, 'LEFT JOIN " . TABLE_PREFIX . "poll AS poll ON (poll.pollid = thread.pollid)', '')
WHERE thread.forumid = " . $forumid . "


ConqSoft 01-20-2007 04:31 PM

What's the error given?

thincom2000 01-20-2007 04:35 PM

Code:

SQL query:

SELECT thread. * , post. * , IF( thread.pollid = poll.pollid, 'poll.*', '' )
FROM forum_thread AS thread
LEFT JOIN forum_post AS post ON ( post.postid = thread.firstpostid ) IF( thread.pollid = poll.pollid, 'LEFT JOIN forum_poll AS poll ON (poll.pollid = thread.pollid)', '' )
WHERE thread.forumid =2
LIMIT 0 , 30

MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF( thread . pollid = poll . pollid , 'LEFT JOIN forum_poll AS poll ON (poll.pol' at line 1

Removed the entire poll table check from the query. All I need is to check if a poll exists anyway, which can be done with thread.pollid

Was returning NULL because I somehow deleted every record in my post table. Good thing I made a backup yesterday.

Now the query works as expected:

Code:

SELECT thread.*, post.*
FROM " . TABLE_PREFIX . "thread AS thread
LEFT JOIN " . TABLE_PREFIX . "post AS post ON (post.postid = thread.firstpostid)
WHERE thread.forumid = " . $forumid . "



All times are GMT. The time now is 03:34 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.00974 seconds
  • Memory Usage 1,722KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete