在SQL视图中添加行数
我正在尝试使用SQL视图创建一个表。它假设在问题表中的每一行中添加一列,该列将具有给定该问题答案的整数值。这就是我到目前为止所做的:在SQL视图中添加行数,sql,database,view,Sql,Database,View,我正在尝试使用SQL视图创建一个表。它假设在问题表中的每一行中添加一列,该列将具有给定该问题答案的整数值。这就是我到目前为止所做的: CREATE VIEW [dbo].[Question] AS SELECT COUNT(answer.Id) as 'Answers', question.Id, question.CreatorId, question.Title, question.Content,
CREATE VIEW [dbo].[Question]
AS
SELECT
COUNT(answer.Id) as 'Answers',
question.Id,
question.CreatorId,
question.Title,
question.Content,
question.CreationDate
FROM
Questions AS question
JOIN
Answers AS answer ON answer.QuestionId = question.Id;
我明白这是不对的,但我想不出还有别的。请帮忙 这不是创建表,而是加入
var commandStr= "If not exists (select name from sysobjects where name = 'Customer') CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime)";
使用SqlCommand=newsqlcommandstr,con
command.ExecuteNonQuery
您需要这样的“我最喜欢的相关子查询”来获取计数:
CREATE VIEW [dbo].[Question]
AS
SELECT (select COUNT(*) from Answers
where QuestionId = question.Id) as 'Answers',
question.Id,
question.CreatorId,
question.Title,
question.Content,
question.CreationDate
FROM Questions AS question;
或者,通过以下方式加入一个组:
CREATE VIEW [dbo].[Question]
AS
SELECT COUNT(answer.Id) as 'Answers',
question.Id,
question.CreatorId,
question.Title,
question.Content,
question.CreationDate
FROM Questions AS question
JOIN Answers AS answer
ON answer.QuestionId = question.Id
GROUP BY question.Id,
question.CreatorId,
question.Title,
question.Content,
question.CreationDate;
请注意,选择列表中的列要么是聚合函数的参数,要么也在GROUP BY子句中列出。如果要计算所有条目,可以使用以下方法:
CREATE VIEW [dbo].[Question] AS
SELECT COUNT(*) AS amount FROM Questions
如果需要更复杂的计数或其他聚合函数,请查看该页面:
粘贴答案时出现错误的选项卡/窗口?效果很好!!谢谢从我添加到问题中的代码中,你可以看到我阅读并理解了这一部分,我知道计数是如何工作的,你提出的站点很棒,但没有答案。