MySQL-----连表
连表:
**拿到两张表的信息**
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-----连表的更多相关文章
- mysql向表中某字段后追加一段字符串:
mysql向表中某字段后追加一段字符串:update table_name set field=CONCAT(field,'',str) mysql 向表中某字段前加字符串update table_n ...
- oracle与mysql创建表时的区别
oracle创建表时,不支持在建表时同时增加字段注释.故采用以下方式: #创建表CREATE TABLE predict_data as ( id integer ), mid ), time dat ...
- mysql数据库表的自增主键号不规律,重新排列
mysql数据库表的自增主键ID乱了,需要重新排序. 原理:删除原有的自增ID,重新建立新的自增ID. 1.删除原有主键: ALTER TABLE `table_name` DROP `id`; 2. ...
- 基于表的数据字典构造MySQL建表语句
表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...
- 用户中心mysql数据库表结构的脚本
/* Navicat MySQL Data Transfer Source Server : rm-m5e3xn7k26i026e75o.mysql.rds.aliyuncs.com Source S ...
- MySQL InnoDB表--BTree基本数据结构
MySQL InnoDB表是索引组织表这一点应该是每一个学习MySQL的人都会首先学到的知识,这代表这表中的数据是按照主键顺序存储,也就是说BTree的叶子节点存储了所有该行的数据. 我最开始是搞Or ...
- mysql数据库表结构导出
mysql数据库表结构导出 命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名; 导出整个数据库结构和数据 mysqldump -h localh ...
- Mysql连表之多对多
说明 这里的文章是接着前面 Mysql连表一对多 写的. 连表多对多 可以理解成一夫多妻和一妻多夫. 男人表: nid name 1 xxx 2 yyy 3 zzz 女人表: nid name 1 a ...
- 在MySQL向表中插入中文时,出现:incorrect string value 错误
在MySQL向表中插入中文时,出现:incorrect string value 错误,是由于字符集不支持中文.解决办法是将字符集改为GBK,或UTF-8. 一.修改数据库的默认字符集 ...
随机推荐
- Ubuntu 18.04 关闭蓝牙开机启动
sudo gedit /etc/rc.local 然后,加入下面一行 rfkill block bluetooth
- JSON WEB TOKEN 从原理到实战
阅读本文大概需要 4.2 分钟. 作者:王廷骏原文:https://juejin.im/post/5ce272c1e51d45109b01b0f8 1. JSON WEB TOKEN 1.1 什么是J ...
- not and or
not and or (逻辑表达式) 首先要做一些准备知识: 1,优先级:逻辑型 < 测试型 < 关系型 < 算数型 2,逻辑型优先级:or < and < not 3, ...
- _bzoj1008 [HNOI2008]越狱【计数】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1008 水题... #include <cstdio> const int mod ...
- Sqlite数据库中索引的使用、索引的优缺点[转]
原文链接1 原文链接2 3. 索引的种类 1)聚集索引:表中行的物理顺序与键值的逻辑(索引)顺序相同.因为数据的物理顺序只能有一种,所以一张表只能有一个聚集索引.如果一张表没有聚集索引,那么这张表 ...
- 转】RMySQL数据库编程指南
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/2/ 感谢! Posted: Sep 24, 2013 Ta ...
- 线程池机制使nginx性能提高9倍
原文标题:Thread Pools in NGINX Boost Performance 9x! 原文官方地址:https://www.nginx.com/blog/thread-pools-boos ...
- bootstrap框架栅格系统使用
使用的前端框架 bootstrap框架 Bootstrap是一个响应式的框架 我们在使用的时候主要使用的是它的网格系统, 1.bootstrap布局 布局容器:.container(用于固定宽度并支 ...
- JSP标签 <meta.....>作用总结
<metahttp-equiv="pragma" content="no-cache"> <metahttp-equiv="cach ...
- sql server 强制关闭连接
USE master; GO DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@SQL+'; KILL '+RTRIM(SPID) FROM ma ...