我有一份调查回复表,其中包括以下选项。 非常同意= 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 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是调查的最后一个问题,也是我列表中的最后一栏)。
提前感谢您的帮助!
答案 0 :(得分:0)
- 这应该是你所需要的。
- 您可以进行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());
-- we now have 2 users, 2 surveys
-- Stan is id=1, Sally is id=2. Ditto for survey numbers starting at 1 and up.
-- in web loop, say PHP or whatever, you are collecting question answers
-- loop thru upon Final Submit and do your inserts into table 'answers' (one row each)
-- Assuming for now surveys themselves for simplicity exist on paper/webpage without questions embedded in database (which is not a problem).
-- But trying to simplify the example. Also note that answers.answer is not null but it can be null (no user answer).
-- One table contains all answers without the need to monkey around with creating columns as you were.
-- That monkeying around would be an answers table for each survey, bad idea.
-- This will perform very fast, and data is easily accessible as if in the format you desire.
-- It may not be readily obvious how, but then again what are followup questions for :>
-- So that question is something like "How do I get at my data" !