PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: January 2014

31 Jan 2014

Use order by before group by in mysql

                       Normally you can use order by before group by in mysql. Now the question is rise, can you use order by before group b in mysql? Yes you can use order by before group by in mysql. It is possible. You've to use subquery for this. ie,

      SELECT * FROM (
                   SELECT * FROM `table` ORDER BY column_name DESC 
                                    )    t
                                       GROUP BY column_name
Where,
               t is alias because you use subquery. If you don't add it, then you'll get error message "Every derived table must have its own alias".


Group by in Mysql:


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

use order by before 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.


Use order by before group by:


                            On above example, you want to select highest count of each id and id should be unique. Then you have to use subquery to select highest count values. The mysql query as follows as:

      SELECT * FROM (
                   SELECT * FROM `table1` ORDER BY count DESC 
                                    )    t
                                       GROUP BY id

where,
                 SELECT * FROM `table1` ORDER BY count DESC  run first. It ordered the data in descending order by count.
                 SELECT * FROM ( ... ) t GROUP BY id it takes unique values.
Now you'll get output like this:
               
order by before group by in mysql

                  Now you'll get id with highest count values.


Related Post:

26 Jan 2014

Add auto_increment to existing column in mysql

                     Auto_ increment is used to increase by 1 while insert a new data to mysql table. The auto increment column must be defined as key.


Create table with auto_increment in mysql:


                      You can add auto_increment while creating new table. The mysql query is:

                       CREATE TABLE table1 
                               (No int(5) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
                                 Game_name varchar(15), Image varchar(15)) 
Now the table is created with auto increment.


Add auto_increment to existing column in mysql:


                      You created a table in which the column with specific definitions in mysql. You created table column without auto increment. Then how can you add it to existing column in mysql.  The following mysql query is used to add auto increment to existing column in mysql.
Mysql Query:
                         Alter table table_name modify column_name datatype(length) AUTO_INCREMENT PRIMARY KEY


Consider the example:
The structure of table1 in database new look like this:


Add auto increment to existing column in mysql


                         Now we are going to add auto increment to column 'No' in mysql table 'table1'.  The mysql query is:
                     Alter table table1 modify No int(5) AUTO_INCREMENT PRIMARY KEY

Now your structure look like this:


Add auto increment to column in mysql


     Now you can see the column name 'No' is auto increment. Suppose  your mysql query like below:

     Alter table table1 modify No int(5) AUTO_INCREMENT

Then you'll get error like this:


mysql error while adding auto increment in mysql


                                 So you should be add 'AUTO_INCREMENT' with 'PRIMARY KEY' in mysql table.

Related Post:

23 Jan 2014

Add primary key to existing column in mysql

                      Primary key is used to avoid duplicate entries in mysql table. A mysql table can have only one primary key.


Create table with primary key in mysql:

                                                The mysql query for create table with primary key is:

                       CREATE TABLE table_name(column_name datatype(length), 
                             PRIMARY KEY(column_name))

For example,
                       CREATE TABLE table1(No int(5) datatype(length), ,Game_name varchar(15),                                     Image varchar(15), PRIMARY KEY(No))

Now the table column 'No' is primary key in this table.

How to add primary key to existing column in mysql:

                         
                              You can add primary while creating new table. But can you add primary key to existing column in mysql? Yes, you can add primary key to existing column in mysql. The mysql query is:
                     
                       Alter table table_name add PRIMARY KEY(column_name)


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:


add primary key in mysql


                    There is no index defined.  Suppose you've to add primary key to column 'Game_name'. On that time you can add the primary key. The mysql query is:

                     Alter table table1 add PRIMARY KEY(Game_name)

Now you click the structure of table in mysql database, the index is defined as below:

add primary key existing column in mysql

                 Now your Game_name column  is changed from NULL into NOT NULL with primary key. Because the primary key should not be null. Only one primary key in a mysql table. A mysql table haven't more than one primary key.

Related Post:
Change column name in mysql
Change column size in mysql table
Delete column in mysql table
Add column after specific field in mysql table
Move columns in mysql table

19 Jan 2014

Change column with NOT NULL to NULL in mysql

                       You created a table in which the column with 'NOT NULL'. Suppose you want to change mysql column definition from 'NOT NULL' to 'NULL'. Then how can you change it in mysql. The following mysql query is used to change column from 'NOT NULL' to 'NULL' in mysql.
Mysql Query:
                         Alter table table_name 
                            change column_name column_name datatype(length) definition


Change column NOT NULL to NULL in mysql:


Consider the example:
The structure of table1 in database new look like this:


change column definition in mysql

       
                Where, the column name 'No' is NOT NULL. You can see the Null column value is No in above mysql table at first row.  Now we are going to change column 'NOT NULL' to 'NULL' in mysql table 'table1'.  The mysql query is:
                 
                           Alter table table1 change No No int(5) NULL

Now your structure look like this:


change column NOT NULL to NULL in mysql


                            Now you can see the column name 'No' is NULL.


Change column NULL to NOT NULL in mysql:


                                         Similarly you can change the column definition from NULL to NOT NULL. The mysql query as follows as:

                                            Alter table table1 change No No int(5) NOT NULL

                        Now the column 'No' changed NULL to NOT NULL.

Related Post:

16 Jan 2014

Rename table in mysql

                       You can change the column name in mysql. Can you change table name in mysql. Yes you can rename mysql table. The mysql query for rename table is:

                       RENAME TABLE old_table_name TO new_name

Consider the following example: The table list in mysql database 'new' is below.


tables in mysql database

                   
                              Now we are going to change the table name 'table1' to table4. The mysql query as follows as:

                       RENAME TABLE table1 TO table4

Now your table list should be like this:


Rename mysql table


                          Now the table name 'table1' is changed into 'table4' in mysql

Related Post:

10 Jan 2014

Difference between the delete, truncate and drop in mysql

DELETE in MySql:

                     1. The mysql command 'DELETE' is used to delete space allocated by mysql server and the structure of table remains same.
                     2. It is removed rows.
                     3. It can be used by either 'WHERE' clause or without it in mysql.
                     4. The data can be roll backed, if it is removed by 'DELETE' mysql command. If the transaction is used which is yet committed before delete the row, then you can roll backed again.

delete in mysql


The mysql query for delete paricular row:

                   DELETE FROM table_name WHERE column_name = condition

Consider the following example:
                     
Delete query in mysql

                  table1 is name of this table. We are going to delete particular row in mysql using 'DELETE' mysql command. The mysql query as follows as:

                 DELETE FROM table1 WHERE id = 3

Now you'll get output like this:

delete particular row in mysql using delete command

                 You can delete whole rows in table. But the structure of table is present in mysql database.
The mysql query for delete table is:

                DELETE FROM table1    

                 Now the mysql table values are deleted from mysql database. Suppose you click table1 in mysql database, then you'll get output in database like this:

delete table in mysql


TRUNCATE in MySql: 

                  1. Remove rows from mysql table but the structure of table remains same.
                  2. The data cannot be roll backed if it is deleted by 'TRUNCATE' mysql command.
                  3. It can be used by only without 'WHERE' clause in mysql.
The mysql query for 'TRUNCATE' as follows as:

                  TRUNCATE TABLE table_name


Truncate mysql command

DROP in MySql: 

                 1.Remove whole table from mysql database.
                 2. The data cannot be roll backed if it is dropped by 'DROP' mysql command.


Drop table in mysql

The mysql query for 'DROP' as follows as:

                  DROP TABLE table_name 

Consider following example. The tables in databases like this:
Drop table in mysql database

Now drop a table 'table1' using 'DROP' mysql command.

              DROP TABLE table1 

Now you'll get output like this:
mysql drop

                Now the table 'table1' removed from mysql table list
Related Post:

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