Jump to content

[SOLVED] PROBLEM IN CREATING TABLE


seyz4all

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
    }
}

Link to comment
Share on other sites

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.