The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
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:
|
#2
|
|||
|
|||
Use LEFT JOIN's.
|
#3
|
|||
|
|||
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 . " |
#4
|
||||
|
||||
What's the error given?
|
#5
|
|||
|
|||
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 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 . " |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|