发布于 2016-03-30 12:13:39 | 177 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Mysql教程,程序狗速度看过来!

Mysql关系型数据库管理系统

MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。


这篇文章主要介绍了在MySQL中使用JOIN语句进行连接操作的详细教程,是MySQL入门学习中的基础知识,需要的朋友可以参考下

到目前,我们已经学习了从一个表中获取数据。这是简单的需要,但在大多数现实MySQL的使用,经常需要将数据从多个表中的一个单一的查询。

可以使用多个表中的单一SQL查询。在MySQL中联接(join)行为是指两个或多个表到一个表中可以使用连接在SELECT,UPDATE和DELETE语句中加入MySQL表。我们将看到一个例子LEFT JOIN简单的MySQL连接。
在命令提示符使用联接:

假设我们两个表的教程tcount_tbl和tutorials_tbl的完整列表如下:
例子:

试试下面的例子:


root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran     |       20 |
| mahnaz     |      NULL |
| Jen       |      NULL |
| Gill      |       20 |
| John Poul    |       1 |
| Sanjay     |       1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|      1 | Learn PHP   | John Poul    | 2007-05-24   |
|      2 | Learn MySQL  | Abdul S     | 2007-05-24   |
|      3 | JAVA Tutorial | Sanjay     | 2007-05-06   |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.00 sec)
mysql>

现在,加入这两个表我们可以编写一个SQL查询。这个查询会选择所有的作者从表tutorials_tbl,从tcount_tbl会拿起相应数量的教程。


mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
  -> FROM tutorials_tbl a, tcount_tbl b
  -> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
|      1 | John Poul    |       1 |
|      3 | Sanjay     |       1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
mysql>

在PHP脚本中使用联接:

可以使用任何上述的SQL查询的PHP脚本。只需要通过PHP函数mysql_query()执行SQL查询,然后用常规方法获取结果。
例子:

试试下面的例子:


<?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.tutorial_id, a.tutorial_author, b.tutorial_count
    FROM tutorials_tbl a, tcount_tbl b
    WHERE a.tutorial_author = b.tutorial_author';

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> ".
     "Tutorial ID: {$row['tutorial_id']} <br> ".
     "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>

MySQL左连接:

一个简单的连接和一个MySQL左连接是不同的。一个MySQL LEFT JOIN提供了额外的考虑到在左边的表。

如果做了LEFT JOIN,得到的所有记录以同样的方式相匹配,此外,得到一个额外的记录每个不匹配的记录,在左表中的联接 - 从而保证了每一个作者得到关联(本例子中):
实例:

试试下面的例子就明白了LEFT JOIN:


root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
  -> FROM tutorials_tbl a LEFT JOIN tcount_tbl b
  -> ON a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
|      1 | John Poul    |       1 |
|      2 | Abdul S     |      NULL |
|      3 | Sanjay     |       1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)

需要做更多的实践才能熟悉JOINS。这是一个复杂的概念,在MySQL/SQL将变得更加清晰。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务