最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-09 08:55:02
文档

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

怎样查询两个表中同一字段的不同数据值:怎样查询两个表中同一字段的不同数据值例如:A表中的字段a有40000条数据B表中的字段a有60000条数据,其中的40000条数据跟A表是一样的怎样能把那不一样的20000条数据查询出来啊?--建表table1,table2: create table table1(id int,na
推荐度:
导读怎样查询两个表中同一字段的不同数据值:怎样查询两个表中同一字段的不同数据值例如:A表中的字段a有40000条数据B表中的字段a有60000条数据,其中的40000条数据跟A表是一样的怎样能把那不一样的20000条数据查询出来啊?--建表table1,table2: create table table1(id int,na

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

例如:

A表中的字段a有40000条数据
B表中的字段a有60000条数据,其中的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

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

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

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

(3)那么获取差值

1select * 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销售人员(即没有销售记录的员工信息列表)。

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

1select userid from bbscs_role_user where roleid = 'sales'

# ---> 11条记录

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

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

# ---> 4条记录

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

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

1select * 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返回结果作为临时表来查询

123select * 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 查询两个表中不同的值(主要是差值) 这个语句查询还是存在问题。

12select 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条记录

测试二:

12select 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排序显示。

123select 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

文档

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

怎样查询两个表中同一字段的不同数据值:怎样查询两个表中同一字段的不同数据值例如:A表中的字段a有40000条数据B表中的字段a有60000条数据,其中的40000条数据跟A表是一样的怎样能把那不一样的20000条数据查询出来啊?--建表table1,table2: create table table1(id int,na
推荐度:
标签: 查询 查找 的数据
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top