This is a discussion on select a column with a condition within the MySQL Database forums, part of the Database Forums category; I need to know if there is a way to work with conditions on colums... e.g. how can I ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I need to know if there is a way to work with conditions on colums...
e.g. how can I take the sum of all "answer" columns? Or how can I select the score of e.g. only question7 (in combination with a query on a question-table?) Don't ask about the table structure... I didn't design it, and it's not possible to change it... CREATE TABLE `answer` ( `individualId` int(11) NOT NULL default '0', `testId` int(11) NOT NULL default '0', `question1` decimal(3,2) NOT NULL default '0.00', `question2` decimal(3,2) NOT NULL default '0.00', `question3` decimal(3,2) NOT NULL default '0.00', `question4` decimal(3,2) NOT NULL default '0.00', `question5` decimal(3,2) NOT NULL default '0.00', `question6` decimal(3,2) NOT NULL default '0.00', `question7` decimal(3,2) NOT NULL default '0.00', `question8` decimal(3,2) NOT NULL default '0.00', `question9` decimal(3,2) NOT NULL default '0.00', PRIMARY KEY (`individualId`,`testId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Thanks! |
|
|||
|
gehegeradeaus@gmail.com wrote: > I need to know if there is a way to work with conditions on colums... > e.g. how can I take the sum of all "answer" columns? Or how can I > select the score of e.g. only question7 (in combination with a query on > a question-table?) > > Don't ask about the table structure... I didn't design it, and it's not > possible to change it... > > CREATE TABLE `answer` ( > `individualId` int(11) NOT NULL default '0', > `testId` int(11) NOT NULL default '0', > `question1` decimal(3,2) NOT NULL default '0.00', > `question2` decimal(3,2) NOT NULL default '0.00', > `question3` decimal(3,2) NOT NULL default '0.00', > `question4` decimal(3,2) NOT NULL default '0.00', > `question5` decimal(3,2) NOT NULL default '0.00', > `question6` decimal(3,2) NOT NULL default '0.00', > `question7` decimal(3,2) NOT NULL default '0.00', > `question8` decimal(3,2) NOT NULL default '0.00', > `question9` decimal(3,2) NOT NULL default '0.00', > PRIMARY KEY (`individualId`,`testId`) > ) ENGINE=MyISAM DEFAULT CHARSET=utf8; > > Thanks! SELECT question1+question2... FROM answer ORDER BY individualId ? SELECT sum(question7) FROM table WHERE... |
|
|||
|
strawberry schreef: > gehegeradeaus@gmail.com wrote: > > > I need to know if there is a way to work with conditions on colums... > > e.g. how can I take the sum of all "answer" columns? Or how can I > > select the score of e.g. only question7 (in combination with a query on > > a question-table?) > > > > Don't ask about the table structure... I didn't design it, and it's not > > possible to change it... > > > > CREATE TABLE `answer` ( > > `individualId` int(11) NOT NULL default '0', > > `testId` int(11) NOT NULL default '0', > > `question1` decimal(3,2) NOT NULL default '0.00', > > `question2` decimal(3,2) NOT NULL default '0.00', > > `question3` decimal(3,2) NOT NULL default '0.00', > > `question4` decimal(3,2) NOT NULL default '0.00', > > `question5` decimal(3,2) NOT NULL default '0.00', > > `question6` decimal(3,2) NOT NULL default '0.00', > > `question7` decimal(3,2) NOT NULL default '0.00', > > `question8` decimal(3,2) NOT NULL default '0.00', > > `question9` decimal(3,2) NOT NULL default '0.00', > > PRIMARY KEY (`individualId`,`testId`) > > ) ENGINE=MyISAM DEFAULT CHARSET=utf8; > > > > Thanks! > > SELECT question1+question2... FROM answer ORDER BY individualId ? > SELECT sum(question7) FROM table WHERE... Thanks for your answer, but that's not exactly what I'm searching for : I need this : SELECT question.questionId, individual.indivudal, EVAL('answer.question'.question.questionId) AS score FROM question, individual , answer WHERE answer.individualId = question.individualId so when question.questionId = 1 -> the query will return the score for question1 |
|
|||
|
gehegeradeaus@gmail.com wrote:
> I need this : > > SELECT question.questionId, individual.indivudal, > EVAL('answer.question'.question.questionId) AS score > FROM question, individual , answer > WHERE answer.individualId = question.individualId > > so when question.questionId = 1 -> the query will return the score for > question1 Does this do what you need? SELECT q.questionId, i.individual, CASE q.questionId WHEN 1 THEN a.question1 WHEN 2 THEN a.question2 WHEN 3 THEN a.question3 WHEN 4 THEN a.question4 WHEN 5 THEN a.question5 WHEN 6 THEN a.question6 WHEN 7 THEN a.question7 WHEN 8 THEN a.question8 WHEN 9 THEN a.question9 END AS score FROM question AS q JOIN individual AS i JOIN answer AS a ON USING (individualId); Regards, Bill K. |
|
|||
|
Bill Karwin schreef: > gehegeradeaus@gmail.com wrote: > > I need this : > > > > SELECT question.questionId, individual.indivudal, > > EVAL('answer.question'.question.questionId) AS score > > FROM question, individual , answer > > WHERE answer.individualId = question.individualId > > > > so when question.questionId = 1 -> the query will return the score for > > question1 > > Does this do what you need? > > SELECT q.questionId, i.individual, > CASE q.questionId > WHEN 1 THEN a.question1 > WHEN 2 THEN a.question2 > WHEN 3 THEN a.question3 > WHEN 4 THEN a.question4 > WHEN 5 THEN a.question5 > WHEN 6 THEN a.question6 > WHEN 7 THEN a.question7 > WHEN 8 THEN a.question8 > WHEN 9 THEN a.question9 > END AS score > FROM question AS q > JOIN individual AS i > JOIN answer AS a ON USING (individualId); > > Regards, > Bill K. Yes it does, thanks! |