Results 1 to 7 of 7

Thread: Where clause matching

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2008
    Posts
    101

    Where clause matching

    how can I return a list of all the rows in the table including null values, as my where clause matching is only true if the value exists in both tables

    SELECT * FROM Items, Stock WHERE Items.StocksId=Stock.StocksId

    the Items table could have a null value for the StocksId so how do I return these values too!

    thanks in advance
    enex

  2. #2
    Lively Member
    Join Date
    Jun 2008
    Location
    Bayang Magiliw, Perlas Ng Silangan
    Posts
    100

    Re: Where clause matching

    try this code:

    Code:
    SELECT * FROM items LEFT JOIN stock	ON items.StockId = stock.StockId

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2008
    Posts
    101

    Re: Where clause matching

    Hi this works well, now I have a requirement to match on ResultsId too! in the items table so I did the following

    Code:
    SELECT * FROM items WHERE ResultsId=1 LEFT JOIN stock ON items.StockId = stock.StockId
    but I go the following error:
    Incorrect syntax near the keyword 'left'.

    How can I match on the results Id too?

    edit this appears to work:

    Code:
    SELECT * FROM items, ItemLevel  LEFT JOIN stock ON items.StockId = stock.StockId WHERE ResultsId=1
    but now Im trying to match on multiple tables where I know values do exist like this:
    Code:
    SELECT * FROM items, ItemLevel  LEFT JOIN stock ON items.StockId = stock.StockId WHERE ResultsId=1
    AND Items.ItemLevelID=ItemLevel.ItemLevelId
    but I get this error:
    The multi-part identifier "ItemLevel.ItemLevelId" could not be bound.
    Last edited by enex; Feb 2nd, 2011 at 07:35 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Where clause matching

    The WHERE clause comes after the FROM clause and the JOIN is part of the FROM clause.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Where clause matching

    You are mixing forms here also. ItemLevel is in the FROM clause (old standard). It should be written like this:
    sql Code:
    1. SELECT
    2.    *
    3. FROM
    4.    items
    5. INNER JOIN ItemLevel  
    6.    ON Items.ItemLevelID=ItemLevel.ItemLevelId
    7. LEFT OUTER JOIN stock
    8.    ON items.StockId = stock.StockId
    9. WHERE ResultsId=1
    Last edited by GaryMazzone; Feb 2nd, 2011 at 08:01 AM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2008
    Posts
    101

    Re: Where clause matching

    Hi Jmc thanks for the reply, I have edited my code to work as you suggested, this works well, I have edited my above post can you help me with my other query?

    thanks in advance
    enex

    EDIT: thanks gary will give it a go!

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Where clause matching

    Also change Left Join to LEFT OUTER JOIN I missed that on my first look
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

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



Click Here to Expand Forum to Full Width