Jump to content

Recommended Posts

Hi all,

 

Am hoping someone can point me in the right direction with this.

 

I am having some problems with the simple addition of columns using the following:-

 

SELECT c1+c2+c3+c4 FROM t1

 

The problem is that the result I am getting is not actually adding the 'actual' figures in t1 columns c1-c4 it is taking there values as 1.  The values are set as c1 - ENUM ('1','0'), c2 - ENUM ('2','0'), c3 - ENUM ('4','0'), and c4 - ENUM ('8','0').  This is giving me table values 1,2,4,8 which I am wanting to add together and insert into another table column.  Why does the above SELECT function not take the 'actual' table values in the addition????

 

Please help! Thanks. - MySQL6

Link to comment
https://forums.phpfreaks.com/topic/131124-solved-problems-with-addition/
Share on other sites

CREATE TABLE  `test`.`enumtest` (
  `idenumtest` int(10) unsigned NOT NULL auto_increment,
  `c1` enum('red','white') NOT NULL,
  `c2` enum('blue','white') NOT NULL,
  PRIMARY KEY  (`idenumtest`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

mysql> INSERT INTO enumtest(c1,c2)
    -> VALUES ('red', 'blue');


mysql> SELECT * FROM enumtest;
+------------+-----+------+
| idenumtest | c1  | c2   |
+------------+-----+------+
|          1 | red | blue |
+------------+-----+------+


mysql> SELECT c1 + c2 FROM enumtest;
+---------+
| c1 + c2 |
+---------+
|       2 |
+---------+

because red and blue each have value 1 in their enum cols.

 

 

Ok Barand, I think I understand now thanks.  How can the enum col values be given different values for different fields? Is ENUM not the correct field type to be using here for what I am trying to achieve?  My aim with this is to set up a Users Access level.  I have two table entries on login, one with user details and the other their account setup type.  The account setup type takes it values (1,0) from checkboxes, and from these values I am trying to set the users access level depending on the addition of the checkbox values. I appreciate your time and help, Thanks again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.