简体   繁体   English

使用左连接和计数查询在MySQL中的选择工作但在MS SQL Server 2012中没有

[英]Query with left join and count in select works in MySQL but not in MS SQL Server 2012

I have the following two tables: 我有以下两个表:

SURVEY 调查
id: INT (PK) id:INT(PK)
title: VARCHAR 标题:VARCHAR
description: TEXT 描述:TEXT

QUESTIONNAIRE 调查问卷
id: INT (PK) id:INT(PK)
title: VARCHAR 标题:VARCHAR
survey_id: INT (FK on survey.id) survey_id:INT(调查中的FK.id)

Screenshot of tables 表的截图

I want to sellect all surveys along with the number of questionnaires that each survey has so i tried the following query: 我想选择所有调查以及每项调查的调查问卷数量,所以我尝试了以下查询:

SELECT s.id, s.title, s.description, count(q.id)  
FROM  survey s  
LEFT OUTER JOIN questionnaire q  ON q.survey_id = s.id  
GROUP BY s.id

I tried the query in MySQL and it works great. 我在MySQL中尝试了这个查询,效果很好。
The problem is i need to use MS SQL SERVER 2012 where the same query does not work. 问题是我需要使用MS SQL SERVER 2012 ,其中相同的查询不起作用。

First i got the following error: 首先我得到以下错误:

Msg 8120, Level 16, State 1, Line 1 Column 'survey.title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 消息8120,级别16,状态1,行1列'survey.title'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中

.

then i tried: 然后我试过:

 SELECT s.id, s.title, s.description, count(q.id)  
 FROM  survey s  
 LEFT OUTER JOIN questionnaire q  
   ON q.survey_id = s.id  
 GROUP BY s.id, s.title, s.description

which reslted in the following error: 在以下错误中重新出现:

Msg 306, Level 16, State 2, Line 4 The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. 消息306,级别16,状态2,行4除非使用IS NULL或LIKE运算符,否则无法比较或排序text,ntext和image数据类型。

If i remove the s.description (which is TEXT) from the SELECT and GROUP BY the query works but this is no solution. 如果我从SELECT和GROUP BY删除s.description(这是TEXT)查询工作,但这不是解决方案。

Any ideas ? 有任何想法吗 ? Thank you 谢谢

I think you should learn how Group By and aggregate functions work. 我认为您应该了解Group By和聚合函数的工作原理。 This would prove greatly effective in your quest to understanding T-SQL. 这对于您理解T-SQL非常有效。 Here is a link that may help you out. 这是一个可以帮助你的链接。

Group By Information 按资料分组

Also, this may not be the case at all but why Text for the description and not varchar in MS SQL Server 2012? 此外,这可能不是这种情况,但为什么说明文本而不是MS SQL Server 2012中的varchar?

text is deprecated - try nvarchar(max) or varchar(max) 不推荐使用text - 尝试nvarchar(max)或varchar(max)

ntext, text, and image (Transact-SQL) ntext,text和image(Transact-SQL)

The left may be the problem but I get you want it. 左边可能是问题,但我得到你想要它。

If nvarchar(max) or varchar(max) does not fix it then try this 如果nvarchar(max)或varchar(max)没有修复它,那么试试这个

SELECT s.id, s.title, s.description, sq.cc  
 FROM  survey s  
 join ( SELECT ss.id, count(qq.id) as cc 
          FROM  survey ss  
          LEFT OUTER JOIN questionnaire q  
                ON q.survey_id = s.id  
         GROUP BY s.id 
      ) sq 
   on sq.id = s.id 

您使用聚合函数和非聚合函数,您必须将表与自身连接,并在要计数的列上使用group by函数

The problem is your Description column type : 问题是您的描述列类型:

Msg 306, Level 16, State 2, Line 4 The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. 消息306,级别16,状态2,行4除非使用IS NULL或LIKE运算符,否则无法比较或排序text,ntext和image数据类型。

If you change the type of the column to nvarchar(max) you will be able to perform your group by. 如果将列的类型更改为nvarchar(max) ,则可以执行分组。

As well as changing the column datatype in the table (or casting to VARCHAR(MAX) at run time) you could also use 除了更改表中的列数据类型(或在运行时转换为VARCHAR(MAX) ),您还可以使用

SELECT s.id,
       s.title,
       s.description,
       COALESCE(q.cnt,0) AS cnt
FROM   survey s
       LEFT OUTER JOIN (SELECT COUNT(q.id) AS cnt,
                               q.survey_id
                        FROM   questionnaire q
                        GROUP  BY q.survey_id) q
                    ON q.survey_id = s.id 

to avoid having to GROUP BY that column at all 完全避免使用GROUP BY那个列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM