连表:

  **拿到两张表的信息**

  select * from userinfo,department

    弊端是数据会乱,出现重复,不建议这样。

  **使userinfo表的part_id列与department的id关联**
  select * from userinfo,department where userinfo.part_id = department.id

  **推荐的连表方式 left/right join(连接,左连接/右连接,join是连接,left是从左边连,right是从右边连)**

  select * from userinfo left join department on userinfo.part_id = department.id

  left是左边的userinfo表的全部显示,意味着department表中如果没有和userinfo相关的就不会被显示出来

  select * from userinfo right join department on userinfo.part_id = department.id

  right是右边的department表的全部显示,意味着userinfo表中如果没有和department相关的就不会被显示出来

  **innder方法,如果连表有一部分是null的就会给隐藏**

  select * from userinfo innder join department on userinfo.part_id = department.id
     userinfo表中没有和department表中相对应的值,会出现null,而innder方法将出现null时一行隐藏

  **统计表中某列的个数,用count统计**

  select count(id) from userinfo;

  **关联多个表**

  select * from

    userinfo

  left join department on userinfo.part_id = department.id

  left join department1 on userinfo.part_id = department.id

  left join department2 on userinfo.part_id = department.id

  left join department3 on userinfo.part_id = department.id

前提是表和表之间是有关系

  

例如:

  select  score.sid, student.sid  from  score

    left join student on score.student_id = student.sid

    left join course on score.course_id = course.cid

    left join class on student.class_id = class.cid

    left join teacher on course.teacher_id=teacher.tid

MySQL-----连表的更多相关文章

  1. mysql向表中某字段后追加一段字符串:

    mysql向表中某字段后追加一段字符串:update table_name set field=CONCAT(field,'',str) mysql 向表中某字段前加字符串update table_n ...

  2. oracle与mysql创建表时的区别

    oracle创建表时,不支持在建表时同时增加字段注释.故采用以下方式: #创建表CREATE TABLE predict_data as ( id integer ), mid ), time dat ...

  3. mysql数据库表的自增主键号不规律,重新排列

    mysql数据库表的自增主键ID乱了,需要重新排序. 原理:删除原有的自增ID,重新建立新的自增ID. 1.删除原有主键: ALTER TABLE `table_name` DROP `id`; 2. ...

  4. 基于表的数据字典构造MySQL建表语句

    表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...

  5. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

  6. 用户中心mysql数据库表结构的脚本

    /* Navicat MySQL Data Transfer Source Server : rm-m5e3xn7k26i026e75o.mysql.rds.aliyuncs.com Source S ...

  7. MySQL InnoDB表--BTree基本数据结构

    MySQL InnoDB表是索引组织表这一点应该是每一个学习MySQL的人都会首先学到的知识,这代表这表中的数据是按照主键顺序存储,也就是说BTree的叶子节点存储了所有该行的数据. 我最开始是搞Or ...

  8. mysql数据库表结构导出

    mysql数据库表结构导出 命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名; 导出整个数据库结构和数据 mysqldump -h localh ...

  9. Mysql连表之多对多

    说明 这里的文章是接着前面 Mysql连表一对多 写的. 连表多对多 可以理解成一夫多妻和一妻多夫. 男人表: nid name 1 xxx 2 yyy 3 zzz 女人表: nid name 1 a ...

  10. 在MySQL向表中插入中文时,出现:incorrect string value 错误

    在MySQL向表中插入中文时,出现:incorrect string value 错误,是由于字符集不支持中文.解决办法是将字符集改为GBK,或UTF-8.      一.修改数据库的默认字符集   ...

随机推荐

  1. CentOS中设置Windows共享文件夹

    在CentOS中设置Samba可实现和Windows共享文件夹.常见的需求:1)用户能够在Windows机器上通过共享文件夹访问远程Linux服务器上自己的主目录:2)用户能够在Windows机器上访 ...

  2. GDI+ 加载PNG图片

    #include <GdiPlus.h>#pragma comment(lib, "GdiPlus.lib")using namespace Gdiplus; clas ...

  3. Tree CodeForces -932D

    错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...

  4. ACM_平面、空间分割问题(递推dp)

    折线分割平面 Time Limit: 2000/1000ms (Java/Others) Problem Description: 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要 ...

  5. 加密解密(3)Bob到CA申请证书过程

    网络安全中最知名的人物大概就是Bob和Alice了,因为很多安全原理阐述中都用这两个虚拟人物来进行实例说明. 我们来看看Bob是怎么从CA中心获得一个数字证书的: 1.Bob首先创建他自己的密钥对(k ...

  6. Android Dialogs(2)最好用DialogFragment创建Dialog

    Creating a Dialog Fragment You can accomplish a wide variety of dialog designs—including custom layo ...

  7. TRUNCATE TABLE 与 DELETE (转)

    TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作.TRUNCATE TABLE 与没有 WHERE 子句的 DELETE 语句类似:但是,TRUNCATE TABLE 速度更快, ...

  8. E. Comments dfs模拟

    http://codeforces.com/contest/747/problem/E 首先,把字符串变成这个样子. hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0 ...

  9. Android 图片文件和Bitmap之间的转换

    String filePath="c:/01.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(filePath); 如果图片过大,可能导致 ...

  10. jsp 访问文件夹中的图片,tomcat配置虚拟目录

    1.配置hosts文件 找到C:\Windows\System32\drivers\etc\hosts.txt 文件 添加127.0.0.1  www.image.com  在dos 命令中执行 pi ...