Jump to content

help with left join command

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
MichaelR

MichaelR

    Newbie

  • Members
  • PipPip
  • 13 posts
Can you please tell me why this will work:

SELECT forum_thread.id, forum_thread.author, forum_thread.name, forum_thread.forum, forum_thread.isSticky, forum_thread.last_post_TS, members.id, members.first_name, members.last_name

        FROM forum_thread

        LEFT JOIN members

        ON forum_thread.author = members.id

        ORDER BY forum_thread.isSticky DESC, forum_thread.last_post_TS DESC


But this will not work:

SELECT forum_thread.id, forum_thread.author, forum_thread.name, forum_thread.forum, forum_thread.isSticky, forum_thread.last_post_TS, members.id, members.first_name, members.last_name

        FROM forum_thread

        LEFT JOIN members

        ON forum_thread.author = members.id, forum_thread.forum = 1

        ORDER BY forum_thread.isSticky DESC, forum_thread.last_post_TS DESC


I need to so that the forum_thread.forum is equal to 1 AND forum_thread.author is equal to members_.id
Please help. :(

#2
MichaelR

MichaelR

    Newbie

  • Members
  • PipPip
  • 13 posts
Okay well I got it to work with:
SELECT forum_thread.id, forum_thread.author, forum_thread.name, forum_thread.forum, forum_thread.isSticky, forum_thread.last_post_TS, members.id, members.first_name, members.last_name

        FROM forum_thread

        LEFT JOIN members

        ON forum_thread.author = members.id

        WHERE forum_thread.forum = 1

        ORDER BY forum_thread.isSticky DESC, forum_thread.last_post_TS DESC

I guess it doesn't allow multiple ON variables?

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Try this:
SELECT forum_thread.id, forum_thread.author, forum_thread.name, forum_thread.forum, forum_thread.isSticky, forum_thread.last_post_TS, members.id, members.first_name, members.last_name
        FROM forum_thread
        LEFT JOIN members
        ON (forum_thread.author = members.id AND forum_thread.forum = 1)
        ORDER BY forum_thread.isSticky DESC, forum_thread.last_post_TS DESC

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
MichaelR

MichaelR

    Newbie

  • Members
  • PipPip
  • 13 posts
Nah, that didn't seem to work WingedPanther; it just shows all threads, not just threads in forum 1.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Ahh... yes, that would require it in the where clause. Putting it in the join clause will just produce null records for those table columns where the forum is not 1.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
MichaelR

MichaelR

    Newbie

  • Members
  • PipPip
  • 13 posts
Thanks though. :)

#7
Amy

Amy

    Newbie

  • Members
  • PipPip
  • 18 posts
hey , try this

SELECT forum_thread.id, forum_thread.author, forum_thread.name, forum_thread.forum, forum_thread.isSticky, forum_thread.last_post_TS, members.id, members.first_name, members.last_name
FROM forum_thread
LEFT JOIN members
ON forum_thread.author = members.id AND forum_thread.forum = 1
ORDER BY forum_thread.isSticky DESC, forum_thread.last_post_TS DESC

i just replaced the comma with AND in ON condition ..

Good Luck !