PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: add column
add column - phponwebsites.com
Showing posts with label add column. Show all posts

17 Dec 2013

Add column after specific field in mysql

                       When you try to add add new column to your table, by default the column should be added to end of your table. But you can add it wherever you want in a table. It is possible in mysql.
Myql Query:
                     alter table tablename add column columnname datatype(length) AFTER specific field name

Add column after particular field in mysql:

               
                    Consider a example. Table1 is name of table. Normally the table look this this:

add column Mysql at phponwebsites


Now you are going to add column after specific column. The mysql query is:
     alter table Table1 add column Game_Tag varchar(30)  AFTER Game_Name

The output look like this:


add column after specific field in Mysql at phponwebsites


Now you will get column 'Game_Tag' is after 'Game_Name'.


Add column at first in mysql:


              Now you are going to add column before specific field. The mysql query is:
    alter table Table1 add column Game_Tag varchar(30) FIRST

The output look like this:

add column first in Mysql at phponwebsites

Now you'll get output as column 'Game_Tag' is present first of your table.


Add multiple columns after specific field in mysql:


               You can add multiple columns after particular column in mysql. The mysql query is:
     alter table Table1
        add column Game_Tag varchar(30),
        add column count int(10),
        add column category varchar(30)
              AFTER Image
The output look like this:


add multiple columns after specific field in Mysql at phponwebsites

13 Dec 2013

Copy values from one column to another within table in mysql

                      Mostly we had copied only particular values from one table to another. Similarly we had copied only particular values within a table. Now the question is rise. That is, can copy the whole column values to another column in same table? Is it possible in mysql?. Yes it is possible in mysql. We can copy the all values in one column to another within a table.
                     Consider a following example:
                                   Table1 is a table name.
                                               
Mysql at phponwebsites
                   
 Now you need to add another column with named as 'Author'. The mysql query is:
                     Alter table Table add column Author varchar(30)

add column in Mysql at phponwebsites

                     You need to copy the Game_Name column values to Author column. The mysql query as follows as:
                              
                               Update tablename set column2=column1

That means, Update Table1 set Author=Game_Name.

Now You will get following as a output:

copy values from one column to another in same table Mysql at phponwebsites

          
                             Now you will get the output both the Game_name and Author column values are same.


Copy values from one column to another except some values within table:


                           Suppose you have to copy all values in a column except some value to another column within a table. It can be done by following mysql query:

                           Update Table1 set Game_Name=Author where Game_Name!='sample1'

Your output look like this:

copy only selected values from one column to another in Mysql at phponwebsites

Now you'll get all values from one column to another except some value.

Related Post: