连接数据库PDO

$user = "root";  //数据库连接账号

$pass = "root";  //数据库连接密码

$dbname = "test";		//数据库名

try {
$db = new PDO("mysql:host=localhost;dbname=$dbname", $user, $pass); //连接数据库
echo "数据库连接成功";
} catch (PDOException $e) {
echo "数据库连接失败";
}

表一:user表

 id name 姓名 sex 性别 age 年龄
1 fan 0 18
2 jack 0 25
3 xiaoming 0 33
4 laowang 1 50
5 julia 0 22
6 pangda 1

53

表二:user_Identity表

id identity_number 身份号码
1 111
2 222
3 3
4 444
5 555
6 666

1、内连接(Inner Join)

$sql = "select * from user as a inner join user_identity as b on a.id = b.identity_number"; //内连接user和user_identity表,as 起别名:user代表a,user_identity代表b

//$sql = "select * from user inner join user_identity on user.id = user_identity.id"; // 效果同上

//$sql = "select a.id,a.name,b.identity_number from user as a inner join user_identity as b on a.id = b.id";  //select 和 from 之间的意思是 - 输出a表的id字段,输出a表的name字段,输出b表的identity_number;

$data = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);

print_r ($data);

输出:

Array
(
[0] => Array
(
[id] => 3
[name] => xiaoming
[sex] => 0
[age] => 33
[identity_number] => 3
) )

2、左连接 (Left Join)

$sql = "select * from user as a left join user_identity as b on a.id = b.identity_number";

$data =  $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);

print_r($data);

输出:

Array
(
[0] => Array
(
[id] => null
[name] => fan
[sex] => 0
[age] => 18
[identity_number] => null
) [1] => Array
(
[id] => null
[name] => jack
[sex] => 0
[age] => 25
[identity_number] => null
) [2] => Array
(
[id] => 3
[name] => xiaoming
[sex] => 0
[age] => 33
[identity_number] => 3
) [3] => Array
(
[id] => null
[name] => laowang
[sex] => 1
[age] => 50
[identity_number] => null
) [4] => Array
(
[id] => null
[name] => julia
[sex] => 0
[age] => 22
[identity_number] => null
) [5] => Array
(
[id] => null
[name] => pangda
[sex] => 1
[age] => 53
[identity_number] => null
) )

3、右连接 (Right Join)

$sql = "select * from user as a right join user_identity as b on a.id = b.identity_number";

$data =  $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);

print_r($data);

输出:

Array
(
[0] => Array
(
[id] => 1
[name] => null
[sex] => null
[age] => null
[identity_number] => 111
) [1] => Array
(
[id] => 2
[name] => null
[sex] => null
[age] => null
[identity_number] => 222
) [2] => Array
(
[id] => 3
[name] => xiaoming
[sex] => 0
[age] => 33
[identity_number] => 3
) [3] => Array
(
[id] => 4
[name] => null
[sex] => null
[age] => null
[identity_number] => 444
) [4] => Array
(
[id] => 5
[name] => null
[sex] => null
[age] => null
[identity_number] => 555
) [5] => Array
(
[id] => 6
[name] => null
[sex] => null
[age] => null
[identity_number] => 666
) )

4、其他连接-省略。。。

mysql常用连接查询的更多相关文章

  1. MySql的连接查询

    类似于oracle的连接查询,mysql连接查询也有左外连接.右外连接.内连接查询.但是,不同的是没有直接 的全外连接查询. 这里介绍MySql的连接查询: 这里已两张表为例:STUDENT 表 和 ...

  2. MySQL查询优化:连接查询排序limit

    MySQL查询优化:连接查询排序limit(join.order by.limit语句) 2013-02-27      个评论       收藏    我要投稿   MySQL查询优化:连接查询排序 ...

  3. MySQL常用的查询命令

    MySQL常用的查询命令 author: headsen chen   2017-10-19  10:15:25 个人原创.转载请注明作者,出处,否则依法追究法律责任 1,查询现在的时间:mysql& ...

  4. mysql常用快速查询修改操作

    mysql常用快速查询修改操作 一.查找并修改非innodb引擎为innodb引擎 # 通用操作 mysql> select concat('alter table ',table_schema ...

  5. 【杂记】mysql 左右连接查询中的NULL的数据筛选问题,查询NULL设置默认值,DATE_FORMAT函数

    MySQL左右连接查询中的NULL的数据筛选问题 xpression 为 Null,则 IsNull 将返回 True:否则 IsNull 将返回 False. 如果 expression 由多个变量 ...

  6. Mysql表连接查询

    原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等 ...

  7. MySQL常见连接查询

    在实际应用中,由于不同的业务需求,一般的select查询语句无法满足要求.所以就需要了解一些MySQL的高级查询方式 内连接 inner join 典型的连接查询,有相等(=)连接和不等(<&g ...

  8. 【mysql】连接查询

  9. MySQL之连接查询

    主要是多表查询和连接查询

随机推荐

  1. ubuntu18.04 安装mysql server

    mysql 5.7支持的最高版本是Ubuntu17 ,即使安装成功后,也会出现各种妖蛾子,本人就被这种问题困扰了好一会.在Ubuntu 18.04下安装mysql,建议安装8.0以上版本! 1. 配置 ...

  2. Android仿淘宝继续上拉进入商品详情页的效果,使用双Fragment动画切换;

    仿淘宝继续上拉进入商品详情页的效果,双Fragment实现: 动画效果: slide_above_in.xml <?xml version="1.0" encoding=&q ...

  3. [Unity算法]点是否在多边形范围内

    参考链接: https://www.zhihu.com/question/26551754 http://www.cnblogs.com/leoin2012/p/6425089.html 原理如下: ...

  4. Aplication的意义和生命周期,与Context的关系,以及关于Aplication和Context相关问题的记录和解决办法

    Context详解地址链接: http://blog.csdn.net/qinjuning/article/details/7310620 Application是一个应用中有且仅有一个的全局共享变量 ...

  5. css一些特殊选择器

    css一些特殊选择器1.在box中,从第几个div开始选择,以后的都会选择到,以下代码表示从#box里面的第二个div开始选择:#box div:nth-of-type(n+2){}2.选择奇数个:d ...

  6. React页面插入script

    项目中遇到插入广告的需要,而广告的信息只是一个url链接,这个链接返回的时一个js,和以前插入广告有点不同.所有找了很多方式. 先来展示广告链接返回的信息: 假设广告链接为:http://192.16 ...

  7. LeetCode OJ 102. Binary Tree Level Order Traversal

    题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...

  8. js原型继承四步曲

    <sctript> //1.创建父类 function Parent(){ this.name = name; } Parent.prototype.age = 20; //2.创建子类 ...

  9. eosjs

    [eosjs] Javascript API,用于帮助访问与 EOSIO RPC API. 1.安装 npm install eosjs@beta 2.Signature Provider The S ...

  10. HBase Snapshot原理和实现

    HBase 从0.95开始引入了Snapshot,可以对table进行Snapshot,也可以Restore到Snapshot.Snapshot可以在线做,也可以离线做.Snapshot的实现不涉及到 ...