seyz4all Posted July 23, 2007 Share Posted July 23, 2007 PLS I WANT TO KNOW HOW TO ADD TWO COLUMNS TOGETHER, THE THIRD COLUMN WOULD HAVE THE SUM EXAMPLE COLUMN C = COLUMN A + COLUMN B I AM NOT TALKING OF SELECT STATEMENT, I MEAN CREATE STATEMNET COST IT IS DONE LIKE THIS IN MSSQL CREATE TABLE [TableName] ( [Column1Name] [int] IDENTITY (1, 1) NOT NULL , [Column2Name] [int] NULL , [Column3Name] AS ([Column1Name] + [Column2Name]) ) ON [PRIMARY] BUT I CANT SEEM TO GET IT IN MYSQL.....PLS HELP ME OUT GO Quote Link to comment Share on other sites More sharing options...
Illusion Posted July 23, 2007 Share Posted July 23, 2007 We can't do that while creating the table.If at all u want a column with the sum of the values in the first and second column use UPDATE statement to set the values in column3, once u have inserted the values. The other way around is Stored Procedures. [Column2Name] [int] NULL there is no need to specify like that as by default at accepts not values and if ur intention is set the values of second coulmn to null values defaultly then [Column2Name] [int] DEFAULT NULL Quote Link to comment Share on other sites More sharing options...
seyz4all Posted July 23, 2007 Author Share Posted July 23, 2007 CAN ANYBODY HELP ME THIS PROBLEM... IF I AM TO USE AN UPDATE SYNTAX, HOW DO I GO ABOUT IT. OR A TRIGGER, PLS HELP ME OUT Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 23, 2007 Share Posted July 23, 2007 (Your Caps lock key is stuck on.) Why would you want to have a column that is merely the sum of two other columns? You could simply calculate the sum any time you needed it and not waste the extra space required for storage of the third value. SELECT *, col1 + col2 AS col3 FROM table; ...where col3 is calculated on-the-fly. Quote Link to comment Share on other sites More sharing options...
seyz4all Posted July 24, 2007 Author Share Posted July 24, 2007 Wildbug thanks a lot man... it worked perfectly.... actually it is the rite thing to do cos nothing is to be inserted into the third column... so its best to select. thank again.. ....................................... i wanna ask, if i am to create a column that would read data from the other column in the case of grade column for school. where A1 represents score from 75-100 i.e it should read the total score column to display A1. do u think its possible and how can i achieve that.. pls help Quote Link to comment Share on other sites More sharing options...
solinent Posted July 24, 2007 Share Posted July 24, 2007 You have to use the SELECT statement, and the value will be calculated on the fly. Columns can't "read" other columns. Could you explain yourself a bit better? If you wanted one column to equal another, you'd follow the same syntax as the other post here. Nevermind, whatever you're asking for should be done with PHP: $result = mysql_query("SELECT grade, name FROM table") or die("Error:".mysql_error()); while ($row = mysql_fetch_assoc($result)) { if ($row['grade'] >= 75) { $lgrade="A1"; } elseif ($row['grade'] >= 50 { $lgrade="B1"; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.