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

7 Jan 2014

Use order by together with group by in mysql

                       You can use both mysql command 'ORDER BY' and 'GROUP BY' separately. The mysql command 'ORDER BY'  is used to sort the column values and 'GROUP BY' is used to eliminate the duplicate values in column.
                        Can you use both 'OREDER BY' and 'GROUP BY' together in a mysql query?
Yes you can use both mysql command in a query.
                        Why you need to use both mysql command in a query?
If you want to eliminate duplicate values in column and also sort the column in a mysql table, then you've to use these two command in single mysql query. ie,

         SELECT * FROM table GROUP BY column_name ORDER BY column_name

You must use 'GROUP BY' before 'ORDER BY' in mysql. Because 'GROUP BY' get higher preference than 'ORDER BY' in mysql.


Select all values with one value is distinct in mysql table:  


                        You can use 'DISTINCT' mysql command for display unique values in only one column. But the situation, you need to select all columns but one column value is distinct in mysql. Then you've to use both 'ORDER BY' and 'GROUP BY' mysql command in a query. The 'ORDER BY' command should be placed after 'GROUP BY' in mysql query.
Consider the following table. table1 is name of this table.

group by and order by in same mysql query

                              Now we are going to get total count of unique values in column 'id' by 'GROUP BY' mysql command and also order by count in ascending order by 'ORDER BY' mysql command. The mysql query as follows as:

               SELECT id,  SUM(count) as count, date FROM table5 
                                  GROUP BY id ORDER BY count DESC

Now you'll get output like this:

using order by and group by together in mysql

                  You can see the output of the above mysql query which displays total count in descending order.

Related Post:

5 Jan 2014

Group by in mysql table

                       If your column in mysql table contains similar values, then you will use 'GROUP BY' mysql command to display unique values. It is used to eliminate the duplicate values in mysql table. It takes only  first row value from top to bottom in mysql table. The mysql query as follows as:

                       SELECT * FROM table_name GROUP BY field_name


Group by in Mysql:


                                      Consider the following table. table1 is name of this table.

group by in mysql

                              Now we are going to get unique values in column 'id' by 'GROUP BY' mysql command like this:

                       SELECT * FROM table1 GROUP BY id 

Now you'll get output like this:

display unique values using group by in mysql


                           You can see the column 'id' values in mysql table 'table1' are unique. The id value 1 are present 1st, 2nd and 5th row. But it takes only the first row.


Group by multiple columns in mysql:


                           You can group the multiple columns in single mysql query.  The mysql query as follows as:

                       SELECT * FROM table1 GROUP BY id , count 

             Where,
                        - The column 'id' got the first preference to group.
                        - Then the column 'count' is grouped.
Your output like this:

group by multiple columns in mysql

                        You can see the mysql table 'table1' which is grouped by both id and count. But you can't the get unique values in both mysql columns.

Related Post:

3 Jan 2014

Order by in mysql table

                      If your column values in mysql table contains more rows, then you can't get the highest and lowest values. On that time, you can sort the column values ascending and descending by 'ORDER BY' mysql command with 'ASC' and 'DESC'. The mysql query for sort column values in mysql table as follwos as:
                       SELECT * FROM table_name ORDER BY field_name ASC( for ascending)
                       SELECT * FROM table_name ORDER BY field_name DESC( for descending)


Order by in Mysql:


                                      Consider the following table. table1 is name of this table.

order by in mysql

                              Now we are going to sort column 'id' in ascending order. The mysql query for this:

                       SELECT * FROM table1 ORDER BY id ASC

Now you'll get output like this:

sorting column values using OREDER BY in mysql

                           You can see the column 'id' values are sorting in ascending order.


Order by multiple columns in mysql:


                           You can sort the multiple columns in single mysql query. The mysql query as follows as:

                       SELECT * FROM table1 ORDER BY id ASC, date DESC 

             Where,
                        - The column 'id' got the first preference to sort
                        - Then the column 'date' is sort
Your output like this:

order by multiple columns in mysql

                        You can see the mysql table 'table1' which is sorted by id in ascending order and date in descending order.

Related Post:

1 Jan 2014

Distinct in mysql

                       The 'DISTINCT' mysql cmmand is used to eliminate duplicate values in a column. The mysql query as follows as:
                   
                      SELECT DISTINCT field_name FROM table_name


Distinct in Mysql:


                                     Consider the following table. table1 is name of this table.

distinct in mysql

                              Now we are going to get unique values in column 'id' by 'DISTINCT' mysql command like this:

                      SELECT DISTINCT id FROM table1

Now you'll get output like this:

unique values using distinct in mysql

                    You can see the table 'table1' which have unique values.


Distinct multiple column in Mysql:


                    You can use multiple column for 'DISTINCT' in a query. But you can't get answer as you want.
The mysql query as follows as:

                      SELECT DISTINCT id, count FROM table1

You'll get output like this:

distinct multiple columns in mysql

29 Dec 2013

Change column datatype in mysql

                       You created a table with column in specific datatype. Sometimes you need to change datatype of column. At the time, you can modify the datatype of field. It is possible in mysql. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name modify column_name datatype(length)


Consider the example:
 table1 is name of this table.

changing column size in mysql at phponwebsites

When you click the structure of table in your database, it is look like this:

Structure of table in mysql

                          Suppose you've to change datatype of column 'Game_name'. On that time you can change the field type. The mysql query is:

                     Alter table table1 modify Game_name char(15)

Now your structure look like this:

change column field in mysql

     Now your Game_name column type is changed from varchar into char.


Changing the multiple column datatype at the time in mysql:


                      You can change multiple column datatype at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    modify No varchar(5),
                                    modify Game_name char(15),
                                    modify Image char(15)

                          Now your output look like this:

change multiple column datatype at a time in mysql

                          Now you will get output column 'No' with varchar, 'Game_name' with char and 'Image' with char in your mysql table.

Datatypes in mysql:

datatypes in mysql

27 Dec 2013

Move columns in mysql table

                       You can change the column name and change the column size in mysql. But can you rearrange the column order in mysql table. Yes, you can rearrange it in mysql. It can be done by 'ALTER', 'CHANGE' and 'AFTER' mysql command. The mysql query for move the column in mysql table is:

                   " ALTER TABLE table_name CHANGE column_name column_name datatype(length) AFTER column_name"

                       You can also done by 'MODIFY' mysql command. The mysql query as follows as:

                   " ALTER TABLE table_name MODIFY column_name datatype(length) AFTER column_name"

Consider the following example: table1 is the name of table.
                   
rearrange columns in mysql

                       Now the column 'name' is moved after column 'img'. The mysql query as follows as:

                   " ALTER TABLE table1 CHANGE name name varchar(30) AFTER img"

Now you'll get output like this:

move column using MODIFY mysql command

The column name 'name' is placed after 'img' in mysql table 'table1'.


Move  multiple columns at the time in mysql:


                             You can move multiple columns at the time in mysql. The column name order id, name ,img in table1 is changed into img, name ,id. The mysql query as follows as:

                         "ALTER TABLE table1 CHANGE id id int(5) AFTER img,
                                         CHANGE name name varchar(30) AFTER img"

Now you'll get output like this:

move multiple columns at a time in mysql using CHANGE command

25 Dec 2013

Changing the column size in mysql

                       You created a table with specific size of column. Sometimes you need to increase the size of column, if the size is not enough. At the time, you can modify the size of field. It is possible in mysql. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name modify column_name datatype(length)


Consider the example:
 table1 is name of this table.

changing column size in mysql at phponwebsites



When you click the structure of table in your database. It is look like this:

srtucture of column in mysql


                          Suppose you've to add more characters to column Game_name. On that time you can change the field size. The mysql query is:
                     Alter table table1 modify Game_name varchar(40)  

Now your structure look like this:

change column size in mysql table

     Now your Game_name column size is changed into 40.


Changing the multiple column size at the time in mysql:


                      You can change multiple column size at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    modify Game_name varchar(50),
                                    modify Image varchar(40)

                          Now your output look like this:

changing multiple column size at a time in Mysql at phponwebsites

                          Now you will get output as Game_name with 50 size and Image with 40 size.

Related Post:

22 Dec 2013

Changing column name in mysql table

                       You created a table with specific column name. Sometimes you need to change the column name. At the time, you can change the field name. It is possible in mysql using by CHANGE mysql command. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name change old_column_name  new_column_name datatype(length)


Consider the example:
 table1 is name of this table.

changing column name in mysql at phponwebsites


                          Suppose you've to change the column name. The mysql query is:
                     Alter table table1 change Game_name  name varchar(40)

Now your table look like this:

change column name using CHANGE mysql command

     Now the column name 'Game_name' is changed into 'name'.


Changing the multiple column name at the time in mysql:


                      You can change multiple column name at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    change No id int(5),
                                    modify Image img varchar(40)

                          Now your output look like this:

change multiple column name at the time in mysql at phponwebsites

                          Now you will get output as No to id and Image to img.

Related Post:

19 Dec 2013

Delete column in mysql table

                       You created a table with number of columns. Sometimes you don't need to specific column. At the time, you can delete the column from mysql table. It is possible in mysql using by DROP mysql command. The following mysql query is used for this:
Mysql Query:
                         Alter table table_name drop column column_name


Consider the example:
 table1 is name of this table.

delete column in mysql table using DROP command


                          Suppose you've to delete the column 'Image'. The mysql query is:
                     Alter table table1 drop column Image

Now your table look like this:

drop column from mysql table

     Now the column name 'Image' is deleted from your mysql table.


Deleting the multiple column at the time in mysql:


                      You can delete multiple column at a time in mysql. The mysql query as follow as:
Mysql Query:
                          Alter table table1
                                    drop column Game_name,
                                    drop column Image

                          Now your output look like this:

Delete multiple column at a time in mysql table

                          Now you will get your mysql table with only column 'No'.

Related Post:

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

15 Dec 2013

Copy values from one database to another in mysql

                       All of you know, you can copy data within a table and between tables. But can you copy data between two database in mysql. Yes, you can done it in mysql. The following mysql query is used for do this.
Mysql Query:
                         INSERT INTO database2.table_name (column_name)
                              SELECT table_name.column_name FROM table_name


Copy data from one database to another in mysql:


                       Consider following example. You've two databases named with db1 and db2. Each database have a table. Table1 is presented in db1 and Table2 is present in db2.
The Table1 in db1 is look like this:

Mysql at phponwebsites

The Table2 in db2 look like this:

Mysql at phponwebsites

Now we copy data from database db1 to db2. The mysql query is:
                     
                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1

Now you will get output as follow as:

copy values from one database to another in Mysql at phponwebsites

                   Now you'll get values in Table2 as like as Table1 in db.

Copy data from one database to another with specific condition in mysql:


                  You can copy only specific values from one database to another in mysql using 'WHERE' condition in mysql. Consider a example: Now we are going to copy named with 'sample1' and 'sample2' from database db1. The mysql query is:

                     INSERT INTO db2.Table2(name,image)
                              SELECT Table1.name, Table2.name FROM Table1
                              WHERE Game_name!='sample3'

Now you'll get output as follow as:

copy values from one database to another with specific condition in Mysql at phponwebsites

Note:
          You can copy the values from one database to another using  this method only if two databases are in same server.

Related Post:

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:

11 Dec 2013

Copy values from one table to another in mysql

                       We can easily copy the values within a table. But can we copy the values from one table to another table?. Yes, you can done it in mysql. The mysql query is:

                       Insert into table2 (column_name1, column_name2) 
                               select column_name1, column_name2 from table1

Consider the example:
 Table1 is name of this table.

Mysql at phponwebsites


Table2 is name of this table.


Mysql at phponwebsites

Copy values from one table1 to table2 in mysql:

                         Now you have to add values from 'Table1' to 'Table2'. The following mysql query is used to do it.
                         Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1

The output of  'Table2' is:

copy values from one table to another in Mysql at phponwebsites

Now you'll get values in 'Table2' which is same as 'Table1'.

Copy values from one table to another except some values in mysql:


                        If you want to some coulmn values, then you will add only required column name only in mysql query. Suppose you need only particular values. Then you use 'where' clause to get your answer. The mysql query as follow as:
                        
                        Insert into Table2 (No,Game_name,Image)
                              select No,Game_Name, Image from Table1  
                                where  Game_Name='sample1' and Game_Name='sample2'

Then the output look like this:


copy specific values from one table to another in Mysql at phponwebsites


Now you will get values only named in sample1 and sample2.

Related Post:

9 Dec 2013

Create mysql database in cpanel

                       1.You have to login to your cpanel to create mysql database.
                       2.Then you need to select mysql databases.

Mysql database and cpanel at phponwebsites

                       3.Type your database name. Then click create database button. Here, my database name is 'new'.

create Mysql database at phponwebsites

Now you'll get successful message as created database successfully as follows as:

create database in cpanel
                   
                          4.Then you have to create new user. Type your user name here.

create Mysql user in database and cpanel at phponwebsites

Then click password generator to get password for your user name in mysql. The pop up box is opened as follows as.


create Mysql user at phponwebsites


Select the checked box and click use password button. Then you the strength password is loaded. Then click create user button.

Mysql database and cpanel at phponwebsites

Now the user name is created.
                   5. Then you need to add your user name to mysql database. Select your user name and database as follows as. then click Add button.

add user to Mysql database and cpanel at phponwebsites

                  6. Now you select all the privileges. Then click Make changes button.

all previleges to mysql user


                     7. Finally you will get a message like this:

Mysql database created in cpanel at phponwebsites

Now you created a mysql database , user in cpanel and add user to mysql database. Then you back to home and click phpmyadmin.


create Mysql database in cpanel


Then you can create table in your mysql database.