select a column with a condition

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 ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-04-2006
gehegeradeaus@gmail.com
 
Posts: n/a
Default select a column with a condition

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!

Reply With Quote
  #2 (permalink)  
Old 12-04-2006
strawberry
 
Posts: n/a
Default Re: select a column with a condition


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...

Reply With Quote
  #3 (permalink)  
Old 12-04-2006
gehegeradeaus@gmail.com
 
Posts: n/a
Default Re: select a column with a condition


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

Reply With Quote
  #4 (permalink)  
Old 12-04-2006
Bill Karwin
 
Posts: n/a
Default Re: select a column with a condition

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.
Reply With Quote
  #5 (permalink)  
Old 12-06-2006
gehegeradeaus@gmail.com
 
Posts: n/a
Default Re: select a column with a condition


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!

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:13 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0