mysql多表查询例子
【理解方式】先分别找出每个表中查询出来的结果,然后再将两个结果合并。
create database test charset utf8 collate utf8_bin;
use test;
create table tb_chengji(
xuesheng varchar(10) not null default '' primary key,
yuwen int(10)
)charset=utf8;
insert into tb_chengji values('亨德森',90),('拉拉纳',100),('马内',115),('库蒂尼奥',99);
create table tb_info(
id int(10) not null primary key auto_increment,
xuesheng varchar(10) not null default '',
xingbie char(1) not null default '',
dizhi varchar(10)select tb_info.id,tb_chengji.xuesheng,xingbie,dizhi,yuwen from tb_chengji,tb_info where tb_chengji.xuesheng=tb_info.xuesheng and tb_chengji.yuwen>=100; not null default ''
)charset=utf8;
insert into tb_info values(1,'马内','男','边锋'),(2,'库蒂尼奥','男','左中场'),(3,'拉拉纳','女','中场'),(4,'亨德森','女','后腰');
多表查询例子:
例子1
select tb_info.id,tb_chengji.xuesheng,xingbie,dizhi,yuwen from tb_chengji,tb_info where tb_chengji.xuesheng=tb_info.xuesheng and tb_chengji.yuwen>=100;
+----+-----------+---------+--------+-------+
| id | xuesheng | xingbie | dizhi | yuwen |
+----+-----------+---------+--------+-------+
| 1 | 马内 | 男 | 边锋 | 115 |
| 3 | 拉拉纳 | 女 | 中场 | 100 |
+----+-----------+---------+--------+-------+
2 rows in set (0.08 sec)
例子2
select tb_info.id,tb_chengji.xuesheng,xingbie,dizhi,yuwen from tb_chengji,tb_info where tb_chengji.yuwen>=100;
+----+-----------+---------+-----------+-------+
| id | xuesheng | xingbie | dizhi | yuwen |
+----+-----------+---------+-----------+-------+
| 1 | 拉拉纳 | 男 | 边锋 | 100 |
| 1 | 马内 | 男 | 边锋 | 115 |
| 2 | 拉拉纳 | 男 | 左中场 | 100 |
| 2 | 马内 | 男 | 左中场 | 115 |
| 3 | 拉拉纳 | 女 | 中场 | 100 |
| 3 | 马内 | 女 | 中场 | 115 |
| 4 | 拉拉纳 | 女 | 后腰 | 100 |
| 4 | 马内 | 女 | 后腰 | 115 |
+----+-----------+---------+-----------+-------+
8 rows in set (0.00 sec)
对上述2例的说明
例1 tb_chengji.xuesheng=tb_info.xuesheng将表tb_chengji和tb_info连接起来,叫做等同连接。
例2 如果不使用tb_chengji.xuesheng=tb_info.xuesheng,那么产生的结果将是两个表的笛卡尔积,叫做全连接。
mysql多表查询例子的更多相关文章
- MySQL多表查询之外键、表连接、子查询、索引
MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...
- MySQL多表查询、事务、DCL:内含mysql如果忘记密码解决方案
MySQL多表查询.事务.DCL 多表查询 * 查询语法: select 列名列表 from 表名列表 where.... * 准备sql # 创建部门表 CREATE TABLE dept( id ...
- Mysql 单表查询 子查询 关联查询
数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...
- (转)Mysql 多表查询详解
MySQL 多表查询详解 一.前言 二.示例 三.注意事项 一.前言 上篇讲到mysql中关键字执行的顺序,只涉及了一张表:实际应用大部分情况下,查询语句都会涉及到多张表格 : 1.1 多表连接有 ...
- MySQL多表查询回顾
----------------------siwuxie095 MySQL 多表查询回顾 以客户和联系人为例(一对多) 1.内连接 /*内连接写法一*/ select * from t_custom ...
- python 3 mysql 单表查询
python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...
- python3 mysql 多表查询
python3 mysql 多表查询 一.准备表 创建二张表: company.employee company.department #建表 create table department( id ...
- Mysql 单表查询-排序-分页-group by初识
Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...
- Mysql 单表查询where初识
Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...
随机推荐
- Bootstrap carousel轮转图的使用
来自:慕课网http://www.imooc.com/code/5395 图片轮播效果在Web中常常能看到,很多人也称之为幻灯片.其主要显示的效果就是多幅图片轮回播放, 从右向左播放,鼠标悬停在图片时 ...
- Jquery 在页面加载后执行的几种方式
1.$(function(){ $("#a").click(function(){ //adding your code here }); }); 2.$(document) ...
- NEC学习 ---- 模块 -简易文字链接列表
为了方便查看, 在最外面设置了一个粉色框. html代码: <div class="container"> <ul class="m-list" ...
- eclipse安装JS插件
在eclipse中有三种Javascript插件可供选择: JSDT JSEclipse Spket Spket插件安装: 手动安装:到http://www.spket.com/download.ht ...
- apple mobile device服务无法启动,错误1053 解决
我不想安装iTunes,于是下了iTunes64安装包,解压后得到6个文件 安装完 AppleMobileDeviceSupport64.msi 发现服务启动不起来,提示错误1053,网上搜了下发现出 ...
- CentOS7配置日志(VirtualBox)
版本为CentOS-Minimal 1.VirtualBox下安装CentOS. 新建虚拟机 下载CentOS,放入盘片,启动虚拟机,按提示开始安装(建议内存1G,硬盘10G以上) 2. 设置网络 ...
- java中日历代码的实现
import java.util.Scanner; com.lv.calendarWatch//包名 /* * 需求:输入一个年份和月份 ,显示当前月日情况 ,星期数要对应准确 * 1.1900年1月 ...
- github 或者gitlab 设置添加SSH, 避免每次提交重复输入用户名
克隆项目二种方式: 1. 使用https url克隆, 复制https url 然后到 git clone https-url 2.使用 SSH url 克隆却需要在克隆之前先配置和添加好 SSH ...
- IIS6(Win2003) 使用.net 4.0 后,默认文档失效解决方案。
IIS6(Win2003) 使用.net framework 4.0 后,默认文档失效解决方案. 用.net framework 4.0 开发的WEB项目,但放到iis6 中无法使用默认文档,状况如下 ...
- sql 时间差
select * from Tickets where ( case when UnloadTime is null then datediff(hh,LoadTime,getdate()) else ...