AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 3258
Accepted
gooddadmike
gooddadmike
Asked: 2011-06-11 09:18:16 +0800 CST2011-06-11 09:18:16 +0800 CST 2011-06-11 09:18:16 +0800 CST

SQL 返回另一个表中选项的答案数为 0

  • 772

在 SO here上得到一个无用的答案后,我的问题就消失了

我需要对其进行优化,因为它会经常被调用。它正在进入一个存储过程。

I am given @slide (tblILDSlide.id) and @sessionid (tblILDSession.id)

我需要我的结果看起来像这样:

--lets say students had answered a(3),b(10),c(0),d(5) in a survey (test)
 |Answer Counts 
0|3
1|10
2|0 --this needs to show as a zero and not be skipped
3|5

由于每个问题都有不同数量的可能答案,因此我需要从此处的 QID 的 @@rowcount of Choice 播种行:

**dbo.tblSurveyAnswerChoice
 QID (int, not null)
 Sequence (int, null)
 Choice (nvarchar(100), null) --this column will vary and is for choices
 --like true/false, Yes, No, Red Blue, 
 --or even Completely Agree,Somewhat Agree...,Completely Disagree

要获得 QID,我必须使用 @slide 和:

**dbo.tblILDSlide
 id (PK, int, not null)
 slide_type(FK, int, not null)
 question_id(FK, int, null)
 media_url(nvarchar(100), null)

我需要为每个选择排一行(没有响应的选择为零)。回复保存在这里:

 --there can be more than one response per question 
 --like for multiple-multiple choice (check all that apply)
**dbo.tblResponses
 response_id(FK, int, not null)
 answer_sequence (int, null) --accepts 1,2,3 etc for choice
 other (nvarchar(50), null) --for text answers

要获得它们,我必须使用 response_id,我可以在此处使用 @sessionid 和 @slide 获得它:

**dbo.tblSlideResponseInfo
 id (PK, int, not null)
 user_id(nvarchar(50), not null)
 slide_id (FK, int, not null)
 response_date (datetime, not null)
 session_id (FK, int, not null)

从 stackoverflow 上的帖子复制: 我的问题是,当有一个可能的选择但没有答案时,我就数不清了。所以我知道我需要抓住可用的可能选择 [在另一个表中......] 我想我需要为 tblSurveyAnswerChoices 中存在的每个答案返回一个空白行然后以某种方式加入其他计数,但是我想不通。

sql-server join
  • 1 1 个回答
  • 911 Views

1 个回答

  • Voted
  1. Best Answer
    krubo
    2011-06-14T19:02:23+08:002011-06-14T19:02:23+08:00

    (从 StackOverflow 上的同一个问题复制我自己的答案)

    您的帖子已经包含一个很好的答案:“我想我需要为 tblSurveyAnswerChoices 中存在的每个答案返回一个空白行,然后以某种方式将其他计数加入其中。”

    这是一种方法:

    select choice, ifnull(countanswervalue,0) 
    from tblsurveyanswerchoice
    left join 
        (select answer_sequence, count(answer_sequence) as countanswervalue
        from tblslideresponses
        group by answer_sequence) 
        as countanswer on countanswer.answer_sequence=tblsurveyanswerchoice.sequence
    /* the where clause is what you already posted */
    where qid in (select question_id from tblidslide where id = @slide)
    

    使用left join确保将列出第一个表中的任何选择。默认情况下,左连接将为第二个表中不存在的项目返回 NULL,但ifnull允许您将 NULL 替换为 0。

    注意:ifnull在 MySQL 中应替换为isnull在 TSQL 中

    • 3

相关问题

  • INNER JOIN 和 OUTER JOIN 有什么区别?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • JOIN 语句的输出是什么样的?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    您如何显示在 Oracle 数据库上执行的 SQL?

    • 2 个回答
  • Marko Smith

    如何选择每组的第一行?

    • 6 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    我可以查看在 SQL Server 数据库上运行的历史查询吗?

    • 6 个回答
  • Marko Smith

    如何在 PostgreSQL 中使用 currval() 来获取最后插入的 id?

    • 10 个回答
  • Marko Smith

    如何在 Mac OS X 上运行 psql?

    • 11 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Marko Smith

    将数组参数传递给存储过程

    • 12 个回答
  • Martin Hope
    Manuel Leduc PostgreSQL 多列唯一约束和 NULL 值 2011-12-28 01:10:21 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Stuart Blackler 什么时候应该将主键声明为非聚集的? 2011-11-11 13:31:59 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST
  • Martin Hope
    BrunoLM Guid vs INT - 哪个更好作为主键? 2011-01-05 23:46:34 +0800 CST
  • Martin Hope
    bernd_k 什么时候应该使用唯一约束而不是唯一索引? 2011-01-05 02:32:27 +0800 CST
  • Martin Hope
    Patrick 如何优化大型数据库的 mysqldump? 2011-01-04 13:13:48 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve