SQL Select Statement - Conditional Join??

Results 1 to 2 of 2

Thread: SQL Select Statement - Conditional Join??

  1. #1
    Join Date
    Dec 1969

    Default SQL Select Statement - Conditional Join??

    Got a question on a SQL statement.<BR>Here&#039s the scenario<BR><BR>2 Tables<BR>- banner_ext<BR>- banner_ext_session<BR><BR>banner_ext fields<BR>- banner_id<BR><BR>banner_ext_session fields<BR>- banner_id<BR>- sessionid<BR><BR>Now I want to print out a total count of the sessionid&#039s found in banner_ext_session. So I did the following code:<BR><BR>SELECT banner_ext.banner_id, count(banner_ext_session.sessionid) AS cnt FROM banner_ext<BR>JOIN banner_ext.banner_id = banner_ext_session.banner_id<BR><BR>This works fine .... kinda<BR>Some of the banner_id&#039s found in banner_ext have a count of 0 in banner_ext_session and thus don&#039t show.<BR><BR>Is their anyway using a join, and not 2 different tables, to get all the banner_id&#039s within banner_ext to show, and have it print out the count for each, even those with a 0 count.<BR><BR>I am lost on this, as if a count is 0, then it&#039s not joining on anything and obviously won&#039t show in my example.<BR><BR>Any help would be appreciated.<BR><BR>Thank You

  2. #2
    Join Date
    Dec 1969

    Default OUTER join...

    You just need to do an OUTER JOIN instead of a simple JOIN.<BR><BR>LEFT OUTER or RIGHT OUTER. (Actually, the OUTER word is optional, since the LEFT/RIGHT JOIN specifies what you are doing.)<BR><BR>I didn&#039t look close at your query to see if you could just replace JOIN with LEFT JOIN, but try that first. If it doesn&#039t work, time to hit the docs.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts