tags:

views:

1761

answers:

3

Having this table

Table "Items"

itemID
itemTitle
itemContent

and this

Table "MyList"

userID
itemID
deleted

how can I select all rows from table "Items" and showing the field "deleted", even if the itemID do not exist in "MyList", given an userID?

Example of the query result:

itemID | itemTitle | deleted | userID
-------------------------------------
1      | Title1    | 1       | 2
2      | Title2    | 0       | 2
3      | Title3    | (null)  | (null)
4      | Title4    | (null)  | (null)

What would be the query, so that I can get that result?

Thanks

+10  A: 
SELECT I.itemID, I.itemTitle, M.deleted
FROM
    Items I
    LEFT OUTER JOIN MyList M ON M.itemID = I.itemID
WHERE M.userID = 9

Edit: Based on OP's comment:

SELECT I.itemID, I.itemTitle, M.deleted, M.userID
FROM
    MyList M
    LEFT OUTER JOIN Items I ON I.itemID = M.itemID
WHERE M.userID = 9
Sean Bright
By doing your query, I only get the rows from Items, that are registered in MyList. What I pretend to do is, get all rows from Items, and the field "deleted", even if this "item" is does not exist in "MyList". I guess the problem is in the "where" condition.
Armadillo
See my edit above.
Sean Bright
A: 

An outer join

select a.itemId, a.itemTitle, b.deleted from Items a left outer join MyList b on a.itemId = b.itemId

Arve
A: 

I'm not sure if this is the best way of doing this, but it returns what I was looking for:

select itemID, itemTitle, deleted, userID
FROM(
SELECT i.id_itemID, i.itemTitle, M.deleted, M.userID
FROM
    MyList M
    right OUTER JOIN Items I ON I.itemID= M.itemID
) as MyTableItems
where itemID = 3 or itemID is null
Armadillo