zoukankan      html  css  js  c++  java
  • mysql-4连接

    联合多表查询

    菜鸟教程join
    日常应用较多的是从多个表格中获取数据。使用join可以在多个表查询进行select、update、delete。
    join按照功能分为三类:

    • inner join(内连接、或等值连接):获取两个表中字段匹配关系的记录
    • left join(左连接):获取左表所有记录,即使右表没有对应匹配的记录
    • right join(右连接):获取右表所有记录,即使左表没有对应匹配的记录。

    如果只写join默认inner join

    1.内连接

    select a.runoob_id,a.runoob_author,b.runoob_count from 
        runoob_tbl a join tcount_tbl b 
        on a.runoob_author = b.runoob_author;
    -- 等价于
    select a.runoob_id,a.runoob_author,b.runoob_count from 
        runoob_tbl a,  tcount_tbl b 
        where a.runoob_author = b.runoob_author;
    

    2.左连接

    SELECT a.runoob_id, a.runoob_author, b.runoob_count 
    	FROM runoob_tbl a LEFT JOIN tcount_tbl b 
    	ON a.runoob_author = b.runoob_author;
    

    3.右连接

    SELECT a.runoob_id, a.runoob_author, b.runoob_count 
    	FROM runoob_tbl a RIGHT JOIN tcount_tbl b 
    	ON a.runoob_author = b.runoob_author;
    

    4.内连接、左连接、右连接区别总结:

    以上示例的准备数据,来自菜鸟

    SET NAMES utf8;
    SET FOREIGN_KEY_CHECKS = 0;
    --  创建表结构`runoob_tbl`
    DROP TABLE IF EXISTS `runoob_tbl`;
    CREATE TABLE `runoob_tbl` (
      `runoob_id` int(11) NOT NULL AUTO_INCREMENT,
      `runoob_title` varchar(100) NOT NULL,
      `runoob_author` varchar(40) NOT NULL,
      `submission_date` date DEFAULT NULL,
      PRIMARY KEY (`runoob_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
    
    --  插入数据
    INSERT INTO `runoob_tbl` VALUES 
        ('1', '学习 PHP', '菜鸟教程', '2017-04-12'), 
        ('2', '学习 MySQL', '菜鸟教程', '2017-04-12'), 
        ('3', '学习 Java', 'RUNOOB.COM', '2015-05-01'), 
        ('4', '学习 Python', 'RUNOOB.COM', '2016-03-06'), 
        ('5', '学习 C', 'FK', '2017-04-05');
    COMMIT;
    
    --  创建表结构 `tcount_tbl`
    DROP TABLE IF EXISTS `tcount_tbl`;
    CREATE TABLE `tcount_tbl` (
      `runoob_author` varchar(255) NOT NULL DEFAULT '',
      `runoob_count` int(11) NOT NULL DEFAULT '0'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    --  插入数据 `tcount_tbl`
    INSERT INTO `tcount_tbl` VALUES 
        ('菜鸟教程', '10'), 
        ('RUNOOB.COM ', '20'), 
        ('Google', '22');
    COMMIT;
    
    SET FOREIGN_KEY_CHECKS = 1;
    
  • 相关阅读:
    有关ubuntu11.04不能正常关机或重启的解决方法
    USRP Experiment 3: Using Airprobe Intercepting GSM Traffic
    GSM900 and GSM1800 ARFCN Frequency
    Linux 下Oracle 开机自启动 与 oratab, dbstart 脚本 说明
    Oracle 网络架构(Networking Architecture) 说明
    Oracle Resource Manager 概述
    Oracle AWR 报告中 No data exists for this section of the report 说明
    Oracle Alerts 与 Metrics(警告与度量)说明
    Oracle Listener 设置密码 示例 说明
    Oracle Resource Manager 概述
  • 原文地址:https://www.cnblogs.com/csj2018/p/9933141.html
Copyright © 2011-2022 走看看