0

我在 Chrome WebDB/SQLite 中这样做

我有一个成员表和一个成员可能已回答或未回答的答案表。

我正在尝试取回包含成员姓名和他们给出的答案的结果集。但如果他们没有给出答案,我仍然希望他们的名字回来。

有人可以尝试帮助我吗?

SELECT m.LastName, 
       m.FirstName, 
       m.MiddleName, 
       s.surveyAnswer 
FROM Members m 
    LEFT JOIN SurveyAnswers s 
      ON s.memberID = m.id 
WHERE m.id = ? 
AND s.eventID = ? 

[results.rows.item(i).memberID, eventId],

这是我尝试的,但仍然只接收回答问题并且也在 SurveyAnswers 表中的成员。

4

4 回答 4

2

成为s.eventID连接的一部分而不是WHERE子句。

您的 select 语句应该看起来更像这样:

SELECT m.LastName, 
       m.FirstName, 
       m.MiddleName, 
       s.surveyAnswer 
FROM Members m 
    LEFT JOIN SurveyAnswers s 
      ON s.memberID = m.id 
        AND s.eventID = ? 
WHERE m.id = ? 
于 2012-07-06T22:34:01.660 回答
1

Joins were recently explained to me in the form of a venn diagram and since then I've been crystal clear on which joins to use in my queries.

Have a look at the page linked below which explains it in the same way and I promise you'll never need to check which join you need again!

visual explanation of SQL joins

于 2012-07-06T22:34:50.053 回答
1

You need to use a LEFT OUTER join, and you also need to specify the condition for SurveyAnswers eventID in the join, not in the where clause. Something like this:

SELECT m.LastName, m.FirstName, m.MiddleName, s.surveyAnswer FROM Members m 
LEFT OUTER JOIN SurveyAnswers s ON (s.memberID = m.id AND s.eventID = ?) 
WHERE m.id = ? 
于 2012-07-06T22:35:51.763 回答
0

您想使用LEFT OUTER JOIN

于 2012-07-06T22:28:45.787 回答