mysql left join查询,比较两个表不同的行

怎样查询两个表中同一字段的不同数据值

例如:

A表中的字段a40000条数据
B
表中的字段a60000条数据,其中的40000条数据跟A表是一样的
怎样能把那不一样的20000条数据查询出来啊?

 

--建表table1,table2   
create   table   table1(id   int,name   varchar(10));  
create   table   table2(id   int,score   int);  
insert   into   table1   select   '1','lee';
insert   into   table1   select   '2','zhang';
insert   into   table1   select   '3','steve';
insert   into   table1   select   '4','wang';  
insert   into   table2   select   '1','90';  
insert   into   table2   select   '2','100';  
insert   into   table2   select   '3','70';  

如表
-------------------------------------------------  
table1   
-------------------------------------------------  
id name 
1 lee
2 zhang

3 steve
4 wang

-------------------------------------------------  

table2

------------------------------------------------- 

id score
1 90
2 100

3 70

------------------------------------------------- 

 

(1)左向外联接的结果集包括   left   outer   子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)  

(2)sql语句  

select * from table1 t1 left join table2 t2 on t1.id = t2.id

-------------结果-------------  
id name id score  
------------------------------  
1 lee 1 90  
2 zhang 2 100  

3 steve 3 70
4 wang null null  
------------------------------  

注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示

(3)那么获取差值

select * from table1 t1 left join table2 t2 on t1.id = t2.id WHERE t2.id is null

-------------结果-------------  
id name id score  

4 wang null null  
------------------------------  

 

下面是工作中实际遇到的情况:

##过滤出0销售人员(即没有销售记录的员工信息列表)。

#销售人员(用户角色中间表)

select userid from bbscs_role_user where roleid = 'sales'

 # ---> 11条记录

 

#统计表(用户销售记录表)

select refid from bbscs_sales_income_stat where type = 4 and month = '2012-02' and amount != 0

 # ---> 4条记录

要求为:另外7个销售人员的记录列出来为目的。

 

##########这个是SQL语句模型 BEGIN##########

select * from b t2 left join a t1 on t1.a1 = t2.b1 WHERE t1.a1 is null

#########这个是SQL语句模型 END############

说明:左表是数据多的那个表(基准表如b表)。left join查询。where条件是右边的那个表(a表)某个字段(a1)为Null作为(判断字段)

 

##SQL返回结果作为临时表来查询

select * from (select userid from bbscs_role_user where roleid = 'sales') t2 left join (select refid from bbscs_sales_income_stat where type = 4 and month = '2012-02' and amount != 0) t1 on t2.userid = t1.refid WHERE t1.refid is null

 # --->7条记录

 

 

测试一:

##SQL语句,mysql 查询两个表中不同的值(主要是差值)  这个语句查询还是存在问题。

select t1.Userid from bbscs_role_user t1 left join bbscs_sales_income_stat t2 on t1.userid = t2.refid and t1.roleid = 'sales' and t2.type = 4 and t2.month = '2012-02' and t2.amount != 0 where t2.id is null; ##表与表,条件与条件独立出来。

 # --->18条记录

 

 

测试二:

select t1.Userid from bbscs_role_user t1 left join bbscs_sales_income_stat t2 on t1.userid = t2.refid and t1.roleid = 'sales' and t2.type = 4 and t2.month = '2012-02' and t2.amount != 0 and t2.id is null ##where or and 区别

 # --->22条记录

 

 

###更为强大的临时表查询功能,将以上查询结果作为一个整体放入。

##跟用户部门中间表关联,按部门id排序显示。

select t4.userid from( select * from (select userid from bbscs_role_user where roleid = 'sales') t2 left join (select refid from bbscs_sales_income_stat where type = 4 and month = '2012-02' and amount != 0) t1 on t2.userid = t1.refid WHERE t1.refid is null ) t3, bbscs_org_user t4 where t3.userid = t4.userid order by orgId

 

### MySQL 中使用 LEFT JOIN 连接三个MySQL 中,`LEFT JOIN` 可用于连接多个。当需要连接三个时,可以通过连续两次 `LEFT JOIN` 来实现。每次加入新时都需要指定其关联条件(即 `ON` 子句)。以下是详细的说明和示例。 #### 语法结构 假设存在三张分别为 `table1`, `table2`, 和 `table3`,它们之间的关系由某些字段决定,则可以按照以下方式构建查询: ```sql SELECT table1.column1, table1.column2, table2.columnA, table2.columnB, table3.columnX, table3.columnY FROM table1 LEFT JOIN table2 ON table1.common_field = table2.common_field LEFT JOIN table3 ON table2.another_common_field = table3.another_common_field; ``` 此查询中: - 首先从 `table1` 开始。 - 使用第一个 `LEFT JOIN` 将 `table2` 添加到结果集中,并基于两者的共同字段建立联系[^2]。 - 接着再用第二个 `LEFT JOIN` 把 `table3` 并入其中,同样依赖于相应的公共字段来定义两者间的关系[^3]。 注意,在最终的结果集里,如果某个右侧被联结的缺少匹配项,那么它所对应的列将会显示为 NULL 值[^1]。 #### 实际例子 考虑下面这个场景:有一个员工信息存储在名为 employees 的;部门详情保存于 departments ;而项目分配情况则记录到了 projects 当中。现在我们希望获取每位雇员的名字连同他们所属部门名称以及正在参与项目的代号列。 假定各结构如下所示: - **employees**: id (INT), name (VARCHAR), department_id (INT) - **departments**: id (INT), dept_name (VARCHAR) - **projects**: emp_id (INT), proj_code (CHAR) 下面是具体的 SQL 查询语句: ```sql SELECT e.name AS EmployeeName, d.dept_name AS DepartmentName, GROUP_CONCAT(p.proj_code SEPARATOR ', ') AS ProjectCodes FROM employees e LEFT JOIN departments d ON e.department_id = d.id LEFT JOIN projects p ON e.id = p.emp_id GROUP BY e.id, d.dept_name; ``` 这里运用了两个层次上的左外连接操作,分别把部门数据与个人资料相挂钩,同时也将人员与其负责的任务进了绑定处理。最后利用聚合函数 `GROUP_CONCAT()` 对多条相关记录进汇总展示。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值