练习:多表合并

多表合并

1.多表纵向合并union

MariaDB [hellodb]> select from teachers union select from students;

2.交叉连接

select from students cross join select from teachers;

3.内连接inner join


MariaDB [hellodb]> select * from students inner join teachers on students.teacherid=teachers.tid;

MariaDB [hellodb]> select stuid,s.name,tid,t.name from students s inner join teachers t on s.teacherid=t.tid;

MariaDB [hellodb]> select stuid,s.name,tid,t.name from students s,teachers t where s.teacherid=t.tid;

1)内连接后过滤数据

select stuid,s.name,tid,t.name from students s inner join teachers t on s.teacherid=t.tid and stuid >3;

4.左外连接


MariaDB [hellodb]> select stuid,s.name,tid,t.name from students as s left outer join teachers as t on s.teacherid=t.tid;

5.右外连接


MariaDB [hellodb]> select stuid,s.name,tid,t.name from students as s right join teachers as t on s.teacherid=t.tid;

6.左外连接扩展用法


MariaDB [hellodb]> select stuid,s.name,teacherid,tid,t.name from students as s left outer join teachers as t on s.teacherid=t.tid where t.tid is null;

7.完全外连接 full outer joion


select from students left join teachers on students.teacherid=teachers.tid
union
select
from students right join teachers on students.teacherid=teachers.tid;

子查询:select 的执行结果,被其它SQL调用
select * from students where age < ( select avg(age) from students );
MariaDB [hellodb]> select stuid,name,age from students where age > (select avg(age) from students);

8.完全外连接的扩展示例


MariaDB [hellodb]> select * from (select s.stuid,s.name s_name,s.teacherid,t.tid,t.name t_name from students s left join teachers t on s.teacherid=t.tid union select s.stuid,s.name s_name,s.teacherid,t.tid,t.name t_name from students s right join teachers t on s.teacherid=t.tid) as a where a.teacherid is null or a.tid is null;

自连接
MariaDB [hellodb]> select emp.name emp_name , leader.name leader_name from employee emp inner join employee as leader on emp.leaderid=leader.id;

三张表连接示例
MariaDB [hellodb]> select st.stuid,st.name,sc.score,co.course from students as st inner join scores sc on st.stuid=sc.stuid inner join courses co on sc.courseid=co.courseid;

原文地址:https://blog.51cto.com/14234933/2388891

时间: 2024-10-26 05:43:40

练习:多表合并的相关文章

两表合并优化

需求: 需要将两个字段有部分相同其他不同,且行数都不相同的datatable根据keycolumn合并为一个table. 相当于做了一个left Join. 方法一: 自己写的最原始的方法 优点:在表合并方面能完美达到效果. 缺点:表很大时(几万行开始)速度相当慢,且容易内存溢出. 1 public static DataTable MergeTable(DataTable dtReturn, DataTable dtMerge, string keyName) 2 { 3 foreach (D

mysql多张表合并一张表进行查找

今天有个需求是 mysql多张表合并一张表查询出该表有多少个userid(去重). 首先我的思路是,把多张表采用union合并成一张表,然后进行group by, 最后进行sum(userid), 得出数量. select COUNT(DISTINCT(`user_id`)) FROM ( select user_id from log_20160706 union select user_id from log_20160707 union SELECT user_id from log_20

把多个工作簿的第一个工作表合并到一个工作簿的多个工作表

功能:把多个工作簿的第一个工作表合并到一个工作簿的多个工作表,新工作表的名称等于原工作簿的名称 Sub Books2Sheets() '定义对话框变量 Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) '新建一个工作簿 Dim newwb As Workbook Set newwb = Workbooks.Add With fd If .Show = -1 Then '定义单个文件变量 Di

考研系列二-线性表合并

套用系列一的线性表类,又加了三个成员函数: 1 #include <fstream> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 6 using namespace std; 7 8 #define N 100 9 enum Status{success,fail,fatal,range_error,overflow}; 10 11 class List{ 12 priv

【原创】如何将多个工作簿中相同格式的工作表合并到一个工作表中

如何将多个工作簿中相同格式的工作表合并到一个工作表中 Sub Books2Sheets() '定义对话框变量 Application.ScreenUpdating = False Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) '新建一个工作簿 Dim newwb As Workbook Set newwb = Workbooks.Add With fd If .Show = -1 The

vba实现excel多表合并

Excel多表合并之vba实现 需求 保留列名,复制每一个excel里的数据,合并到一个excel 操作步骤 将要合并的文件放在同一文件夹下,复制过来就好(ps:最好不要直接操作原数据文件,避免操作失败,数据丢失) 在这个目录下创建一个“合并.xlsx” 双击打开“合并.xlsx” 同时按 ALT + F11 出现下图,按图中文字操作即可完成合并 完成 附录代码 Sub 合并当前目录下所有工作簿的全部工作表() Dim MyPath, MyName, AWbName Dim Wb As Work

mysql多表合并为一张表

有人提出要将4张表合并成一张.数据量比较大,有4千万条数据.有很多重复数据,需要对某一列进行去重. 思路: 1.导出数据.select into outfile. 2.传输到要导入数据的服务器上. 3.创建一张新表,主键和约束等与那三张表一样. 4.LOAD DATA INFILE导入.利用replace参数进行覆盖达到去重的目的. LOAD DATA INFILE '/backup/tbl_get.csv' replace INTO TABLE emob.tbl_new; LOAD DATA

大数据技术之MapReduce中多表合并案例

大数据技术之MapReduce中多表合并案例 1)需求: 订单数据表t_order: id pid amount 1001 01 1 1002 02 2 1003 03 3 订单数据order.txt 1001 01 1 1002 02 2 1003 03 3 1004 01 4 1005 02 5 1006 03 6 商品信息表t_product pid pname 01 小米 02 华为 03 格力 商品数据pd.txt 01 小米 02 华为 03 格力 将商品信息表中数据根据商品pid合

Excel多表合并的宏

Sub 合并当前目录下所有工作簿的全部工作表() Dim MyPath, MyName, AWbName Dim Wb As Workbook, WbN As String Dim G As Long Dim Num As Long Dim BOX As String Application.ScreenUpdating = False MyPath = ActiveWorkbook.Path MyName = Dir(MyPath & "\" & "*.xl