What if we only want to empty the data inside the table, and not to delete the table definition in this case we use TRUNCATE TABLE statement:
Syntax:
|
1 |
TRUNCATE TABLE table_name |
What if we only want to empty the data inside the table, and not to delete the table definition in this case we use TRUNCATE TABLE statement:
Syntax:
|
1 |
TRUNCATE TABLE table_name |
MySQL UNION Keyword allows you to combine two or more result sets from multiple tables or queries together.
MySQL UNION removes all duplicate rows from the result set
Syntax:
|
1 2 3 4 |
SELECT statement UNION [DISTINCT | ALL] SELECT statement UNION [DISTINCT | ALL] |
The BETWEEN operator is used in combination with WHERE clause match or select a range of data value between two values.
Syntax:
|
1 2 3 4 |
SELECT <field1, field2,...> FROM <table_name> WHERE <field_name> BETWEEN value1 AND value2 |
Consider “emp_info” table:
| empID | empName | sale |
|---|---|---|
| 1 | Mark | 1000 |
| 2 | Max | 1500 |
| 3 | Mark | 1800 |
| 5 | John | 1500 |
|
1 2 3 |
SELECT * FROM emp_info WHERE SALE BETWEEN 1000 AND 2000; |
Output:
| empID | empName | sale |
|---|---|---|
| 1 | Mark | 1000 |
| 2 | Max | 1500 |
| 3 | Mark | 1800 |
| 4 | Mark | 3000 |
| 5 | John | 1500 |
The IN clause is used to match multiple values in a WHERE clause.
Syntax:
|
1 2 3 |
SELECT <field1, field2,...> FROM <table_name> WHERE <field> IN (value1,value2,...) |
Consider “emp_info” table:
| empID | empName | sale |
|---|---|---|
| 1 | Mark | 1000 |
| 2 | Max | 1000 |
| 3 | Mark | 1000 |
| 4 | Mark | 1000 |
| 5 | John | 1000 |
|
1 2 |
SELECT * FROM emp_info WHERE EMPNAME IN ('Mark','Max') |
Output:
| empID | empName | sale |
|---|---|---|
| 1 | Mark | 1000 |
| 2 | Max | 1000 |
| 3 | Mark | 1000 |
| 4 | Mark | 1000 |
The GROUP BY statement is used to group the result set returned by SELECT statement by one or more columns.We can use GROUP BY to group values of a column we can perform any calculations on that column. COUNT, SUM, AVG etc. function can be applied on the grouped column.
Consider below “emp_info” table
| empID | empName | sale |
|---|---|---|
| 1 | Mark | 1000 |
| 2 | Max | 1000 |
| 3 | Mark | 1000 |
| 4 | Mark | 1000 |
| 5 | John | 1000 |
|
1 2 |
SELECT EMPNAME,SUM(SALE) FROM emp_info GROUP BY EMPNAME |
Output:
| empName | sale |
|---|---|
| Mark | 3000 |
| Max | 1000 |
| John | 1000 |
We have seen SQL SELECT command along with WHERE clause to fetch data from MySQL table. But when we try to give a condition which compare field or column value to NULL it does not work properly.
To handle such situation MySQL provides three operators
Conditions involving NULL are special. You cannot use = NULL or != NULL to look for NULL values in columns. Such comparisons always fail because it’s impossible to tell whether or not they are true. Even NULL = NULL fails.
To look for columns that are or are not NULL, use IS NULL or IS NOT NULL.
Suppose a table tcount_tbl in TUTORIALS database and it contains two columns tutorial_author and tutorial_count, where a NULL tutorial_count indicates that the value is unknown:
Try out following examples:
|
You can see that = and != do not work with NULL values as follows:
|
To find records where the tutorial_count column is or is not NULL, the queries should be written like this:
|
You can use if…else condition to prepare a query based on NULL value.
Following example take tutorial_count from outside and then compare it with the value available in the table.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } if( isset($tutorial_count )) { $sql = 'SELECT tutorial_author, tutorial_count FROM tcount_tbl WHERE tutorial_count <strong>=</strong> $tutorial_count'; } else { $sql = 'SELECT tutorial_author, tutorial_count FROM tcount_tbl WHERE tutorial_count <strong>IS</strong> $tutorial_count'; } mysql_select_db('TUTORIALS'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Author:{$row['tutorial_author']} <br> ". "Count: {$row['tutorial_count']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysql_close($conn); ?> |
The result set returned by the SELECT Query is free from any sort order unless you specify it in the SELECT statement.ORDER BY clause is used to sort the result set by a specified column or columns.The ORDER BY clause sorts the records in ascending order by default. To sort the result set in a descending order, use the DESC keyword.
Syntax:
|
1 2 3 |
SELECT <field1, field2,...> FROM <table_name> ORDER BY <field1, field2,...> ASC|DESC |
The LIKE operator is used in a WHERE clause to search for a specified pattern of string in a column value, WHERE clause with equal sign (=) works fine where we want to do an exact match. Like if “emp_name = ‘deepmala'”. But there may be a requirement where we want to filter out all the results where emp_name should contain “mala”. This can be handled using SQL LIKE clause along with WHERE clause.
If LIKE clause is used along with % characters then sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
Without a % character LIKE clause works similar to equal sign along with WHERE clause.
Syntax:
|
1 2 3 |
SELECT <column_name> FROM <table_name> WHERE <column_name> LIKE <pattern> |
The “emp_info” table:
| empID | empName |
|---|---|
| 1 | smita |
| 2 | sneha |
| 3 | jyoti |
| 4 | pooja |
Now we want to select the employees whose name starts with character “s” from the table above then the query would be like below.
|
1 2 |
SELECT * FROM emp_info WHERE EMPNAME LIKE 's%'; |
OUTPUT:
| empID | empName |
|---|---|
| 1 | smita |
| 2 | sneha |
If you want to delete a record from any MySQL table then you can use SQL command DELETE FROM.
|
1 |
DELETE FROM table_name [WHERE Clause] |
WHERE clause can be used to delete specified rows, if WHERE clause is not specified then all rows get deleted.
The MySQL UPDATE statement is used to update column values of existing rows in a table with new values.Specific columns can be modified using the SET clause by supplying new values for those column. MySQL UPDATE statement can be used with WHERE clause to specify the conditions for the rows to update. Without using WHERE clause, all rows are updated.
|
1 2 |
UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause] |
In the previous chapter SELECT statement is used to fetch data from MySQL table. Here we can add WHERE clause to filter out results. Using WHERE clause we can specify a selection condition to select only those records which fulfills the specified condition.
Syntax:
|
1 2 |
SELECT field1, field2,...fieldN FROM table_name1, table_name2... [WHERE condition1 [AND [OR]] condition2..... |
A WHERE clause can be used alongwith DELETE or UPDATE SQL command also.
The SQL SELECT command is used to fetch data from MySQL database.
|
1 2 3 |
SELECT field1, field2,...fieldN FROM table_name1, table_name2... [WHERE Clause] [OFFSET M ][LIMIT N] |
|
1 2 |
SELECT * FROM <table_name> |
|
1 2 |
SELECT <column1,column2,...> FROM <table_name> |
|
1 2 3 4 5 6 7 |
SELECT <column1,column2,...> FROM <table_name> WHERE <column1> operator <value> //For all columns SELECT * FROM <table_name> WHERE <expression> |
|
1 2 3 4 |
SELECT * FROM <table_name> WHERE <column1> operator <value> AND <column2> operator <value>; |
|
1 2 3 4 |
SELECT * FROM <table_name> WHERE <column1> operator <value> OR <column2> operator <value>; |
While fetching data you can write as complex SQL as you like. Procedure will remain same as mentioned above.
The LIMIT clause can be used to limit the number of rows to be returned by the SELECT query. LIMIT takes one or two numeric arguments, which must both be non negative integer constants.With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
|
1 2 3 |
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15 SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows |