×

MariaDB 教程

MariaDB 介绍MariaDB 安装MariaDB 创建表MariaDB 删除表MariaDB 插入查询MariaDB 管理MariaDB 选择查询MariaDB PHP语法MariaDB Where子句MariaDB 建立连接MariaDB 更新查询MariaDB 创建数据库MariaDB 删除查询MariaDB 删除数据库MariaDB like子句MariaDB 选择数据库MariaDB ORDER BY排序子句MariaDB 数据类型MariaDB Join数据联接语句MariaDB 空值MariaDB 备份方法MariaDB 正则表达式MariaDB 备份加载方法MariaDB 事务MariaDB 实用功能MariaDB 表更改命令MariaDB 索引和统计表MariaDB 临时表MariaDB 克隆表MariaDB 序列MariaDB 管理重复MariaDB SQL注入保护

MariaDB 相关资源

MariaDB 外部资源MariaDB 相关讨论

MariaDB 版本更新

MariaDB 10.1.26发布

MariaDB Join数据联接语句


在之前的讨论和示例中,我们检查了从单个表中检索,或从多个来源检索多个值。 大多数现实世界的数据操作要复杂得多,需要从多个表进行聚合,比较和检索。

JOIN允许将两个或多个表合并到单个对象中。 它们通过SELECT,UPDATE和DELETE语句使用。

使用JOIN查看语句的一般语法如下所示 -

SELECT column
FROM table_name1
INNER JOIN table_name2
ON table_name1.column = table_name2.column;

注意JOINS的旧语法使用隐式连接和没有关键字。 可以使用WHERE子句来实现联接,但关键字最适合可读性,维护和最佳实践。

JOIN有许多形式,如左连接,右连接或内连接。 各种连接类型基于共享值或特性提供不同类型的聚合。

在命令提示符或PHP脚本中使用JOIN。

命令提示符

在命令提示符下,只需使用标准语句 -

root@host# mysql -u root -p password;
Enter password:*******
mysql> use PRODUCTS;
Database changed

mysql> SELECT products.ID_number, products.Nomenclature, inventory.inventory_ct
   FROM products
   INNER JOIN inventory
   ON products.ID_numbeer = inventory.ID_number;
+-------------+----------------+-----------------+
| ID_number   | Nomenclature   | Inventory Count |
+-------------+----------------+-----------------+
| 12345       | Orbitron 4000  | 150             |
+-------------+----------------+-----------------+
| 12346       | Orbitron 3000  | 200             |
+-------------+----------------+-----------------+
| 12347       | Orbitron 1000  | 0               |
+-------------+----------------+-----------------+

PHP脚本使用JOIN

使用mysql_query()函数执行连接操作 -

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

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

   $sql = 'SELECT a.product_id, a.product_manufacturer, b.product_count   
      FROM products_tbl a, pcount_tbl b 
      WHERE a.product_manufacturer = b.product_manufacturer';

   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 "Manufacturer:{$row['product_manufacturer']} <br> ".
         "Count: {$row['product_count']} <br> ".
         "Product ID: {$row['product_id']} <br> ".
         "--------------------------------<br>";
   }

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

成功的数据检索后,您将看到以下输出 -

ID Number: 12345
Nomenclature: Orbitron 4000
Inventory Count: 150
--------------------------------------
ID Number: 12346
Nomenclature: Orbitron 3000
Inventory Count: 200
--------------------------------------
ID Number: 12347
Nomenclature: Orbitron 1000
Inventory Count: 0
--------------------------------------
mysql> Fetched data successfully

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)