Detailed description of the usage of the WHERE clause in MySQL, detailed description of the mysqlwhere clause

Source: Internet
Author: User

Detailed description of the usage of the WHERE clause in MySQL, detailed description of the mysqlwhere clause

We can see that the SQL SELECT command is used to obtain data from the MySQL table. We can use the filter results in the WHERE clause of a Condition Clause. Using the WHERE clause, we can specify a selection criterion to select the required records from the table.
Syntax:

The following is a common SQL WHERE clause used to obtain data from MySQL table's SELECT command syntax:

SELECT field1, field2,...fieldN table_name1, table_name2...[WHERE condition1 [AND [OR]] condition2.....
  • You can use one or more tables separated by commas (,), including various WHERE clause conditions. However, the SELECT command of the WHERE clause is an optional part.
  • You can use the WHERE clause to specify any conditions.
  • You can specify more than one condition to use the and or operator.
  • SQL commands that can be used in a WHERE clause to distribute DELETE or UPDATE specify conditions.

Just like in programming languages, if the WHERE clause of a condition. This clause is used to compare the Field Values in the MySQL table with the given values. If the external value is equal to the available field value in the MySQL table, the row is returned.

The WHERE clause can be used for listing operators.

Assume that field A has A value of 10 and field B has A value of 20:

The WHERE clause is very useful when you want to obtain rows in the selection table, especially when you use MySQL JOIN. JOIN is discussed in another chapter.

This is a common practice is to use the primary key to search records quickly.

If the given condition does not match the record in any table, no rows will be returned for the query.
Obtain data from the command prompt:

This will use the WHERE clause of the SQL SELECT command to obtain the selected data MySQL table tutorials_tbl
Instance:

In the following example, the author name in the tutorials_tbl table is Sanjay and all records are returned:

root@host# mysql -u root -p password;Enter password:*******mysql> use TUTORIALS;Database changedmysql> SELECT * from tutorials_tbl WHERE tutorial_author='Sanjay';+-------------+----------------+-----------------+-----------------+| tutorial_id | tutorial_title | tutorial_author | submission_date |+-------------+----------------+-----------------+-----------------+|      3 | JAVA Tutorial | Sanjay     | 2007-05-21   |+-------------+----------------+-----------------+-----------------+1 rows in set (0.01 sec)mysql>

Unless the LIKE comparison is case-insensitive. The search area is case sensitive. The BINARY keyword is as follows.

root@host# mysql -u root -p password;Enter password:*******mysql> use TUTORIALS;Database changedmysql> SELECT * from tutorials_tbl \     WHERE BINARY tutorial_author='sanjay';Empty set (0.02 sec)mysql>

Use a PHP script to obtain data:

You can use the WHERE clause of the SELECT command for the same SQL to go to functionmysql_query () of PHP. This function is used to execute SQL commands. Another PHP function mysql_fetch_array () can be used to obtain all selected data. The row returned by this function acts as an associated array, number array, or two. This function returns FALSE if no more rows exist.
Instance:

In the following example, the author name in the tutorials_tbl table is Sanjay and all records are returned:

<?php$dbhost = 'localhost:3036';$dbuser = 'root';$dbpass = 'rootpassword';$conn = mysql_connect($dbhost, $dbuser, $dbpass);if(! $conn ){ die('Could not connect: ' . mysql_error());}$sql = 'SELECT tutorial_id, tutorial_title,         tutorial_author, submission_date    FROM tutorials_tbl    WHERE tutorial_author="Sanjay"';//by www.jb51.netmysql_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 "Tutorial ID :{$row['tutorial_id']} <br> ".     "Title: {$row['tutorial_title']} <br> ".     "Author: {$row['tutorial_author']} <br> ".     "Submission Date : {$row['submission_date']} <br> ".     "--------------------------------<br>";} echo "Fetched data successfully\n";mysql_close($conn);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.