Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

mysql - SQL - Select rows from two different tables

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...