2015-09-01 45 views
0

我有一個應用程序,詢問用戶多個問題。目標是不顯示用戶已經回答的問題。所以,如果鮑勃回答問題X,他永遠不會看到它。然而,如果她還沒有回答,莎莉會看到問題X.續集原始mysql查詢返回空字段

我有一個MySQL查詢,在我的查詢編輯器內工作得很好。查詢連接兩個表,並返回與提供的用戶名字符串不匹配的問題,或者該用戶名爲空(表示該問題尚未回答)。如果用戶名字符串爲空,則問題在sequelize中查詢返回空字段,但如果問題已被其他用戶名回答,則會顯示該問題。

這裏是我的工作查詢:

select * from questions 
left join responses on responses.QuestionId = questions.id 
where responses.username <> 'username string' or responses.username is null 
order by rand() limit 1; 

這裏是我的sequelize查詢:

_sequelize.query(
'select * from questions ' + 
'left join responses on responses.QuestionId = questions.id ' + 
'where responses.username <> "' + req.query.name + '" or responses.username is null ' + 
'order by rand() ' + 
'limit 1', 
{model : models.Responses}) 
+0

嘗試在您的Sequelize選項中添加「logging:console.log」,以便您可以看到生成的SQL。 – HeadCode

+0

Null Response:Executing(default):select * from questions left join responses on responses.QuestionId = questions.id where response.username <>「teej」or responses.username is null order by rand()limit 1 – ibtjw

+0

我想你需要對您的問題進行重新說明以使其更清楚。 「查詢返回空字段是什麼意思?」當然,大部分字段將爲空。你是說所有的字段都是空的嗎?爲什麼用戶名字段爲空?你應該檢查你的代碼。 – HeadCode

回答

0

用戶錯誤。我有我的關聯錯誤,所以我在問題表中添加了「已答覆」字段。