【问题标题】:Sequelize raw mysql query returning null fieldsSequelize原始mysql查询返回空字段
【发布时间】:2015-09-01 04:00:15
【问题描述】:

我有一个向用户询问多个问题的应用。目标是不显示用户已经回答的问题。因此,如果 Bob 回答问题 X,他将永远看不到它。但是,如果 Sally 尚未回答问题 X,她会看到问题。

我有一个 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.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})

【问题讨论】:

  • 尝试将“logging:console.log”添加到您的 Sequelize 选项中,以便您可以看到生成的 SQL。
  • 空响应:执行(默认):从问题中选择*左加入responses.QuestionId = questions.id,其中responses.username “teej”或responses.username是rand( ) 限制 1
  • 我认为您需要改写您的问题以使其更清晰。 “查询返回空字段”是什么意思?当然,大多数字段将为空。你是说所有字段都是空的吗?为什么用户名字段会为空?你应该在你的代码中检查它。

标签: mysql node.js sequelize.js


【解决方案1】:

用户错误。我的关联有误,所以我在 Questions 表中添加了一个“beenAnswered”字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多