Mysql 计算有利百分比
我有一个调查答复表,范围包括以下选择。 强烈同意=5,同意=4,中立=3,不同意=2,强烈不同意=1 该表看起来有点像这样:Mysql 计算有利百分比,mysql,Mysql,我有一个调查答复表,范围包括以下选择。 强烈同意=5,同意=4,中立=3,不同意=2,强烈不同意=1 该表看起来有点像这样: ID: Q1 Q2 Q3 Q4 Qn 1 5 4 5 3 2 2 4 5 2 1 4 3 4 4 3 2 3 4 5 4 3 4 3 我使用下面的MySQL代码获取一个列,并为百分比有利、中性和不利生成3个附加列 Select (case when Q1 = 5 or Q
ID: Q1 Q2 Q3 Q4 Qn
1 5 4 5 3 2
2 4 5 2 1 4
3 4 4 3 2 3
4 5 4 3 4 3
我使用下面的MySQL代码获取一个列,并为百分比有利、中性和不利生成3个附加列
Select
(case when Q1 = 5 or Q1 = 4 then 1 else null end) as Q1Fav,
(case when Q1 = 3 then 1 else null end) as Q1Neu,
(case when Q1 = 2 or Q1 = 1 then 1 else null end) as Q1UnFav
From survey_data
这给了我一列中Q1的有利响应,另一列中的中性响应,等等
有没有办法扩展它,让MySQL继续生成这些列,直到它到达Qn?(其中Qn是调查的最后一个问题,也是我列表中的最后一列)
提前感谢您的帮助 ——这应该是您所需要的全部
--你可以进行900万次调查
--一个可以有200个问题,另一个可以有4个。不管怎样
create table users
(
userId int auto_increment primary key,
fName varchar(100) not null, -- Stan
lName varchar(100) not null -- Smith
)ENGINE=MyIsam;
create table surveys
(
surveyId int auto_increment primary key,
sTitle varchar(255) not null,
dtCreated date not null -- date created in system
)ENGINE=MyIsam;
-- create table questions
-- ( -- to keep things simple to start, assume questions are not in mysql
-- that can be for version Beta 0.02
-- );
create table UserTakesSurvey
( -- A user takes a survey (this is going to be useful)
-- 1 row for each combo kinda obvious
userId int not null,
surveyId int not null,
dtTaken date not null,
-- Foreign Key constraints go here:
-- to Users Table:
FOREIGN KEY (userId) REFERENCES users(userId),
-- to Surveys Table:
FOREIGN KEY (surveyId) REFERENCES surveys(surveyId),
-- so no row can exist here with a wrong (non-existing) userId or surveyId
-- addition indexes here:
primary key (userid,surveyid),
key (surveyid,userid)
)ENGINE=MyIsam;
create table answers
( id int auto_increment primary key,
userId int not null,
surveyId int not null, -- survey id
qNum int not null, -- question number
answer int not null, -- 5 to 1
-- Foreign Key constraints go here:
-- to Users Table:
FOREIGN KEY (userId) REFERENCES users(userId),
-- to Surveys Table:
FOREIGN KEY (surveyId) REFERENCES surveys(surveyId),
-- so no row can exist here with a wrong (non-existing) userId or surveyId
-- Additional indexes go here:
-- create a composite (combo) key incorporating two columns
KEY (userId,surveyId),
KEY (surveyId,userId)
)ENGINE=MyIsam;
insert users (fName,lName) values ('Stan','Smith'),('Sally','Higgins');
-- first survey will have 7 questions hypothetically, 2nd 101 questions
insert surveys (sTitle,dtCreated) values ('Feelings about Milk','2013-01-17');
insert surveys (sTitle,dtCreated) values ('Thoughts on Global Warming',curdate());
--这应该是你所需要的
--你可以进行900万次调查
--一个可以有200个问题,另一个可以有4个。不管怎样
create table users
(
userId int auto_increment primary key,
fName varchar(100) not null, -- Stan
lName varchar(100) not null -- Smith
)ENGINE=MyIsam;
create table surveys
(
surveyId int auto_increment primary key,
sTitle varchar(255) not null,
dtCreated date not null -- date created in system
)ENGINE=MyIsam;
-- create table questions
-- ( -- to keep things simple to start, assume questions are not in mysql
-- that can be for version Beta 0.02
-- );
create table UserTakesSurvey
( -- A user takes a survey (this is going to be useful)
-- 1 row for each combo kinda obvious
userId int not null,
surveyId int not null,
dtTaken date not null,
-- Foreign Key constraints go here:
-- to Users Table:
FOREIGN KEY (userId) REFERENCES users(userId),
-- to Surveys Table:
FOREIGN KEY (surveyId) REFERENCES surveys(surveyId),
-- so no row can exist here with a wrong (non-existing) userId or surveyId
-- addition indexes here:
primary key (userid,surveyid),
key (surveyid,userid)
)ENGINE=MyIsam;
create table answers
( id int auto_increment primary key,
userId int not null,
surveyId int not null, -- survey id
qNum int not null, -- question number
answer int not null, -- 5 to 1
-- Foreign Key constraints go here:
-- to Users Table:
FOREIGN KEY (userId) REFERENCES users(userId),
-- to Surveys Table:
FOREIGN KEY (surveyId) REFERENCES surveys(surveyId),
-- so no row can exist here with a wrong (non-existing) userId or surveyId
-- Additional indexes go here:
-- create a composite (combo) key incorporating two columns
KEY (userId,surveyId),
KEY (surveyId,userId)
)ENGINE=MyIsam;
insert users (fName,lName) values ('Stan','Smith'),('Sally','Higgins');
-- first survey will have 7 questions hypothetically, 2nd 101 questions
insert surveys (sTitle,dtCreated) values ('Feelings about Milk','2013-01-17');
insert surveys (sTitle,dtCreated) values ('Thoughts on Global Warming',curdate());
德鲁,非常感谢你的帮助。我听不懂你的回答。我是MySQL的新手,非常感谢您的帮助。没问题,让我添加更多内容需要15分钟谢谢。如果有帮助的话,如果不清楚的话,我正在处理的表格中的每一条记录都代表一个调查对象,列代表调查问题本身。我知道。我正在介绍我认为更聪明的设计,并将解释原因。你可以把它放在架子上思考一下,如果这些答案有帮助,然后向上投票,如果很糟糕,然后向下投票,绿色复选标记回答,所以它对你(和其他人都知道)关闭Drew,非常感谢你的帮助。我听不懂你的回答。我是MySQL的新手,非常感谢您的帮助。没问题,让我添加更多内容需要15分钟谢谢。如果有帮助的话,如果不清楚的话,我正在处理的表格中的每一条记录都代表一个调查对象,列代表调查问题本身。我知道。我正在介绍我认为更聪明的设计,并将解释原因。你可以把它放在一个架子上,思考一下这些答案是否有用,然后向上投票,如果很糟糕,然后向下投票,绿色复选标记回答,这样它就对你关闭了(其他人也知道)