Mariadb 简明教程

MariaDB - Select Query

在本章中,我们将学习如何从表中选择数据。

In this chapter, we will learn how to select data from a table.

SELECT 语句检索选定的行。它们可以包括 UNION 语句、排序子句、LIMIT 子句、WHERE 子句、GROUP BY…​HAVING 子句和子查询。

SELECT statements retrieve selected rows. They can include UNION statements, an ordering clause, a LIMIT clause, a WHERE clause, a GROUP BY…​HAVING clause, and subqueries.

复习以下一般语法 −

Review the following general syntax −

SELECT field, field2,... FROM table_name, table_name2,... WHERE...

SELECT 语句提供了多种指定所用表的选择 −

A SELECT statement provides multiple options for specifying the table used −

  1. database_name.table_name

  2. table_name.column_name

  3. database_name.table_name.column_name

所有选择语句都必须包含一个或多个 select expressions 。选择表达式由以下选项之一组成 −

All select statements must contain one or more select expressions. Select expressions consist of one of the following options −

  1. A column name.

  2. An expression employing operators and functions.

  3. The specification “table_name.*” to select all columns within the given table.

  4. The character “*” to select all columns from all tables specified in the FROM clause.

可以在执行选择语句时使用命令提示符或 PHP 脚本。

The command prompt or a PHP script can be employed in executing a select statement.

The Command Prompt

在命令提示符处,如下执行语句 −

At the command prompt, execute statements as follows −

root@host# mysql -u root -p password;
Enter password:*******
mysql> use PRODUCTS;
Database changed
mysql> SELECT * from products_tbl
+-------------+---------------+
| ID_number   | Nomenclature  |
+-------------+---------------+
| 12345       | Orbitron 4000 |
+-------------+---------------+

PHP Select Script

在 PHP 函数中使用相同的 SELECT 语句来执行操作。您将再次使用 mysql_query() 函数。查看以下给出的示例 −

Employ the same SELECT statement(s) within a PHP function to perform the operation. You will use the mysql_query() function once again. Review an example given below −

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'SELECT product_id, product_name,product_manufacturer, ship_date FROM products_tbl';
   mysql_select_db('PRODUCTS');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
      echo "Product ID :{$row['product_id']} <br> ".
         "Name: {$row['product_name']} <br> ".
         "Manufacturer: {$row['product_manufacturer']} <br> ".
         "Ship Date : {$row['ship_date']} <br>".
         "--------------------------------<br>";
   }

   echo "Fetched data successfully\n";
   mysql_close($conn);
?>

当数据检索成功时,您将看到以下输出 −

On successful data retrieval, you will see the following output −

Product ID: 12345
Nomenclature: Orbitron 4000
Manufacturer: XYZ Corp
Ship Date: 01/01/17
----------------------------------------------
Product ID: 12346
Nomenclature: Orbitron 3000
Manufacturer: XYZ Corp
Ship Date: 01/02/17
----------------------------------------------
mysql> Fetched data successfully

最佳实践建议在每次 SELECT 语句之后释放游标内存。PHP 提供了 mysql_free_result() 函数用于此目的。查看其用法,如下所示 −

Best practices suggest releasing cursor memory after every SELECT statement. PHP provides the mysql_free_result() function for this purpose. Review its use as shown below −

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'SELECT product_id, product_name, product_manufacturer, ship_date FROM products_tbl';
   mysql_select_db('PRODUCTS');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
      echo "Product ID :{$row[0]} <br> ".
         "Name: {$row[1]} <br> ".
         "Manufacturer: {$row[2]} <br> ".
         "Ship Date : {$row[3]} <br> ".
         "--------------------------------<br>";
   }

   mysql_free_result($retval);
   echo "Fetched data successfully\n";
   mysql_close($conn);
?>