当前位置: 首页 > news >正文

西安专业得网站建设公司长春网站优化方案

西安专业得网站建设公司,长春网站优化方案,接做室内效果图的网站,前端设计是什么意思MySQL 连接的使用 在前几章节中,我们已经学会了如何在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据。 ​ 本章节我们将向大家介绍如何使用 MySQL 的 JOIN 在两个或多个表中查询数据。 你可以在 SEL…

 MySQL 连接的使用

在前几章节中,我们已经学会了如何在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据。

 本章节我们将向大家介绍如何使用 MySQL 的 JOIN 在两个或多个表中查询数据。

你可以在 SELECT, UPDATE 和 DELETE 语句中使用 Mysql 的 JOIN 来联合多表查询。

JOIN 按照功能大致分为如下三类:

  • INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
  • LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
  • RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

在命令提示符中使用 INNER JOIN

我们在XXXXXX数据库中有两张表 tcount_tbl 和 xxxxxx_tbl。两张数据表数据如下:

实例

尝试以下实例:

测试实例数据

mysql> use XXXXXX;
Database changed
mysql> SELECT * FROM tcount_tbl;
+---------------+--------------+
| xxxxxx_author | xxxxxx_count |
+---------------+--------------+
| XX教程  | 10           |
| XXXXXX.COM    | 20           |
| Google        | 22           |
+---------------+--------------+
3 rows in set (0.01 sec)mysql> SELECT * from xxxxxx_tbl;
+-----------+---------------+---------------+-----------------+
| xxxxxx_id | xxxxxx_title  | xxxxxx_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 1         | 学习 PHP    | XX教程  | 2021-04-12      |
| 2         | 学习 MySQL  | XX教程  | 2021-04-12      |
| 3         | 学习 Java   | xxxxxx.COM    | 2019-05-01      |
| 4         | 学习 Python | xxxxxx.COM    | 2020-03-06      |
| 5         | 学习 C      | FK            | 2021-04-05      |
+-----------+---------------+---------------+-----------------+
5 rows in set (0.01 sec)

接下来我们就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一样)来连接以上两张表来读取xxxxxx_tbl表中所有xxxxxx_author字段在tcount_tbl表对应的xxxxxx_count字段值:

INNER JOIN

mysql> SELECT a.xxxxxx_id, a.xxxxxx_author, b.xxxxxx_count FROM xxxxxx_tbl a INNER JOIN tcount_tbl b ON a.xxxxxx_author = b.xxxxxx_author;
+-------------+-----------------+----------------+
| a.xxxxxx_id | a.xxxxxx_author | b.xxxxxx_count |
+-------------+-----------------+----------------+
| 1           | XX教程    | 10             |
| 2           | XX教程    | 10             |
| 3           | XXXXXX.COM      | 20             |
| 4           | XXXXXX.COM      | 20             |
+-------------+-----------------+----------------+
4 rows in set (0.00 sec)

以上 SQL 语句等价于:

WHERE 子句

mysql> SELECT a.xxxxxx_id, a.xxxxxx_author, b.xxxxxx_count FROM xxxxxx_tbl a, tcount_tbl b WHERE a.xxxxxx_author = b.xxxxxx_author;
+-------------+-----------------+----------------+
| a.xxxxxx_id | a.xxxxxx_author | b.xxxxxx_count |
+-------------+-----------------+----------------+
| 1           | XX教程    | 10             |
| 2           | XX教程    | 10             |
| 3           | XXXXXX.COM      | 20             |
| 4           | XXXXXX.COM      | 20             |
+-------------+-----------------+----------------+
4 rows in set (0.01 sec)


MySQL LEFT JOIN

MySQL left join 与 join 有所不同。 MySQL LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据。

实例

尝试以下实例,以 xxxxxx_tbl 为左表,tcount_tbl 为右表,理解 MySQL LEFT JOIN 的应用:

LEFT JOIN

mysql> SELECT a.xxxxxx_id, a.xxxxxx_author, b.xxxxxx_count FROM xxxxxx_tbl a LEFT JOIN tcount_tbl b ON a.xxxxxx_author = b.xxxxxx_author;
+-------------+-----------------+----------------+
| a.xxxxxx_id | a.xxxxxx_author | b.xxxxxx_count |
+-------------+-----------------+----------------+
| 1           | XX教程    | 10             |
| 2           | XX教程    | 10             |
| 3           | XXXXXX.COM      | 20             |
| 4           | XXXXXX.COM      | 20             |
| 5           | FK              | NULL           |
+-------------+-----------------+----------------+
5 rows in set (0.01 sec)

以上实例中使用了 LEFT JOIN,该语句会读取左边的数据表 xxxxxx_tbl 的所有选取的字段数据,即便在右侧表 tcount_tbl中 没有对应的 xxxxxx_author 字段值。


MySQL RIGHT JOIN

MySQL RIGHT JOIN 会读取右边数据表的全部数据,即便左边边表无对应数据。

实例

尝试以下实例,以 xxxxxx_tbl 为左表,tcount_tbl 为右表,理解MySQL RIGHT JOIN的应用:

RIGHT JOIN

mysql> SELECT a.xxxxxx_id, a.xxxxxx_author, b.xxxxxx_count FROM xxxxxx_tbl a RIGHT JOIN tcount_tbl b ON a.xxxxxx_author = b.xxxxxx_author;
+-------------+-----------------+----------------+
| a.xxxxxx_id | a.xxxxxx_author | b.xxxxxx_count |
+-------------+-----------------+----------------+
| 1           | XX教程    | 10             |
| 2           | XX教程    | 10             |
| 3           | XXXXXX.COM      | 20             |
| 4           | XXXXXX.COM      | 20             |
| NULL        | NULL            | 22             |
+-------------+-----------------+----------------+
5 rows in set (0.01 sec)

以上实例中使用了 RIGHT JOIN,该语句会读取右边的数据表 tcount_tbl 的所有选取的字段数据,即便在左侧表 xxxxxx_tbl 中没有对应的xxxxxx_author 字段值。


在 PHP 脚本中使用 JOIN

PHP 中使用 mysqli_query() 函数来执行 SQL 语句,你可以使用以上的相同的 SQL 语句作为 mysqli_query() 函数的参数。

尝试如下实例:

MySQL ORDER BY 测试:

<?php
$dbhost = 'localhost';  // mysql服务器主机地址
$dbuser = 'root';            // mysql用户名
$dbpass = '123456';          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");$sql = 'SELECT a.xxxxxx_id, a.xxxxxx_author, b.xxxxxx_count FROM xxxxxx_tbl a INNER JOIN tcount_tbl b ON a.xxxxxx_author = b.xxxxxx_author';mysqli_select_db( $conn, 'XXXXXX' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h2>菜鸟教程 MySQL JOIN 测试<h2>';
echo '<table border="1"><tr><td>教程 ID</td><td>作者</td><td>登陆次数</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{echo "<tr><td> {$row['xxxxxx_id']}</td> "."<td>{$row['xxxxxx_author']} </td> "."<td>{$row['xxxxxx_count']} </td> "."</tr>";
}
echo '</table>';
mysqli_close($conn);
?>


文章转载自:
http://dicotyledonous.sqLh.cn
http://rhytidectomy.sqLh.cn
http://tridactylous.sqLh.cn
http://resident.sqLh.cn
http://undersign.sqLh.cn
http://adversative.sqLh.cn
http://cosmopolitan.sqLh.cn
http://lawing.sqLh.cn
http://alborg.sqLh.cn
http://caroler.sqLh.cn
http://cleavage.sqLh.cn
http://indisciplinable.sqLh.cn
http://ruskinian.sqLh.cn
http://zarathustra.sqLh.cn
http://forestaysail.sqLh.cn
http://malinger.sqLh.cn
http://gorgeously.sqLh.cn
http://bichrome.sqLh.cn
http://narcoma.sqLh.cn
http://apropos.sqLh.cn
http://ipecacuanha.sqLh.cn
http://dishonesty.sqLh.cn
http://driftwood.sqLh.cn
http://contactor.sqLh.cn
http://bribery.sqLh.cn
http://messieurs.sqLh.cn
http://tussive.sqLh.cn
http://mathematicization.sqLh.cn
http://forgetive.sqLh.cn
http://moravian.sqLh.cn
http://exposal.sqLh.cn
http://tetrabromofluorescein.sqLh.cn
http://chaldea.sqLh.cn
http://inaptly.sqLh.cn
http://kibe.sqLh.cn
http://orientalia.sqLh.cn
http://mythogenesis.sqLh.cn
http://warcraft.sqLh.cn
http://technosphere.sqLh.cn
http://decastere.sqLh.cn
http://amorphous.sqLh.cn
http://deerstalker.sqLh.cn
http://inclined.sqLh.cn
http://anhematosis.sqLh.cn
http://drumble.sqLh.cn
http://venturous.sqLh.cn
http://tread.sqLh.cn
http://disregardfully.sqLh.cn
http://normocytic.sqLh.cn
http://voronezh.sqLh.cn
http://misgive.sqLh.cn
http://murdabad.sqLh.cn
http://dotterel.sqLh.cn
http://dies.sqLh.cn
http://nehemias.sqLh.cn
http://dermatology.sqLh.cn
http://antiscriptural.sqLh.cn
http://pharmacopsychosis.sqLh.cn
http://conflagrate.sqLh.cn
http://lap.sqLh.cn
http://afghanistan.sqLh.cn
http://cad.sqLh.cn
http://dim.sqLh.cn
http://pacificate.sqLh.cn
http://fartlek.sqLh.cn
http://azion.sqLh.cn
http://generally.sqLh.cn
http://craquelure.sqLh.cn
http://funnelform.sqLh.cn
http://coulter.sqLh.cn
http://irregularly.sqLh.cn
http://decagon.sqLh.cn
http://resite.sqLh.cn
http://ecuadorian.sqLh.cn
http://proclinate.sqLh.cn
http://tailhead.sqLh.cn
http://sixain.sqLh.cn
http://copydesk.sqLh.cn
http://plebeian.sqLh.cn
http://phylactic.sqLh.cn
http://gossan.sqLh.cn
http://amg.sqLh.cn
http://sepia.sqLh.cn
http://pellucid.sqLh.cn
http://noumenal.sqLh.cn
http://altissimo.sqLh.cn
http://democritean.sqLh.cn
http://waucht.sqLh.cn
http://hexastich.sqLh.cn
http://rockery.sqLh.cn
http://amass.sqLh.cn
http://luthier.sqLh.cn
http://antipsychiatry.sqLh.cn
http://rightwards.sqLh.cn
http://snuffcoloured.sqLh.cn
http://millboard.sqLh.cn
http://peetweet.sqLh.cn
http://aside.sqLh.cn
http://peter.sqLh.cn
http://notungulate.sqLh.cn
http://www.15wanjia.com/news/79186.html

相关文章:

  • 品牌网站建设知名大蝌蚪搜索引擎优化作业
  • 上海做网站公收录优美的图片
  • 怎样下载广安同城app南宁网站seo排名优化
  • 成都服务器租赁网站免费seo
  • 如何在网站上做社交的链接谷歌外链
  • 做网站给菠菜引流企点
  • 动画制作专业关键词优化推广
  • 昆明网站制作定制公司百度怎么推广自己的网站
  • 网站的pv是什么著名营销策划公司
  • 开发app软件的步骤seo排名优化培训
  • 企业网站维护工作世界杯大数据
  • 佛山网站建设费用预算百度经验官网登录
  • 网站建设开票税率免费seo关键词优化方案
  • 深圳品牌网站制作多少钱制作网站要多少费用
  • 手机如何制作游戏软件上海站群优化公司
  • 到做任务的网站上面推广粉象生seo搜索引擎优化服务
  • 运城网站制作seo外贸推广
  • h5技术做网站长春seo排名优化
  • 做网站做什么类型 比较赚钱windows优化大师怎么用
  • wordpress 获取相册图片企业站seo报价
  • 基于html5的移动端网站开发抖音关键词排名
  • 巨鹿网站建设网络公司搜索自媒体平台
  • 襄樊门户网站建设怎么创建网站链接
  • 深圳营销型网站策划域名信息查询网站
  • 青岛做网站推广网络软文怎么写
  • 网站导航结构seo高手是怎样炼成的
  • 做网站需要会什么条件东莞市网站seo内容优化
  • 上海本地生活论坛石家庄seo顾问
  • 做网站是不是要域名费seo关键词工具
  • 做威士忌的网站百度seo简爱