|
-
Feb 1st, 2011, 11:06 AM
#1
Thread Starter
Lively Member
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
-
Feb 1st, 2011, 11:56 AM
#2
Lively Member
Re: Where clause matching
try this code:
Code:
SELECT * FROM items LEFT JOIN stock ON items.StockId = stock.StockId
-
Feb 2nd, 2011, 07:22 AM
#3
Thread Starter
Lively Member
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.
-
Feb 2nd, 2011, 07:27 AM
#4
Re: Where clause matching
The WHERE clause comes after the FROM clause and the JOIN is part of the FROM clause.
-
Feb 2nd, 2011, 07:53 AM
#5
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:
SELECT
*
FROM
items
INNER JOIN ItemLevel
ON Items.ItemLevelID=ItemLevel.ItemLevelId
LEFT OUTER JOIN stock
ON items.StockId = stock.StockId
WHERE ResultsId=1
Last edited by GaryMazzone; Feb 2nd, 2011 at 08:01 AM.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Feb 2nd, 2011, 07:53 AM
#6
Thread Starter
Lively Member
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!
-
Feb 2nd, 2011, 08:01 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|