关于 mysql 中的 select * from table_a,table_b 的问题

最近接受公司同事原先负责的项目,在看代码的时候,看到了以下的sql语句:

select * from table_a,table_b where ….

刚开始看到的时候,只是感觉有点眼熟,好像之前在哪里见到过,但是仔细想想,也说不出什么所以然来。后来测试了一下,下面总结一下。

1:select * from table_a,table_b //不使用where条件的前提下

假设当前表table_a的数据如下:

table_a_idtable_a_nametable_a_infotable_a_texttable_a_desc
1table_a_1table_a_info_1table_a_text_1table_a_desc_1
2table_a_2table_a_info_2table_a_text_2table_a_desc_2

当前表table_b的数据如下:

table_b_idtable_b_nametable_b_infotable_b_texttable_b_desc
1table_b_1table_b_info_1table_b_text_1table_b_desc_1
2table_b_2table_b_info_2table_b_text_2table_b_desc_2
得到的结果如下:
table_a_idtable_a_nametable_a_infotable_a_texttable_a_desctable_b_idtable_b_nametable_b_infotable_b_texttable_b_desc
1table_a_1table_a_info_1table_a_text_1table_a_desc_11table_b_1table_b_info_1table_b_text_1table_b_desc_1
1table_a_1table_a_info_1table_a_text_1table_a_desc_12table_b_2table_b_info_2table_b_text_2table_b_desc_2
2table_a_2table_a_info_2table_a_text_2table_a_desc_21table_b_1table_b_info_1table_b_text_1table_b_desc_1
2table_a_2table_a_info_2table_a_text_2table_a_desc_22table_b_2table_b_info_2table_b_text_2table_b_desc_2
如果我们将sql语句修改为: select * from table_b,table_a,得到的结果如下:
table_b_idtable_b_nametable_b_infotable_b_texttable_b_desctable_a_idtable_a_nametable_a_infotable_a_texttable_a_desc
1table_b_1table_b_info_1table_b_text_1table_b_desc_11table_a_1table_a_info_1table_a_text_1table_a_desc_1
1table_b_1table_b_info_1table_b_text_1table_b_desc_12table_a_2table_a_info_2table_a_text_2table_a_desc_2
2table_b_2table_b_info_2table_b_text_2table_b_desc_21table_a_1table_a_info_1table_a_text_1table_a_desc_1
2table_b_2table_b_info_2table_b_text_2table_b_desc_22table_a_2table_a_info_2table_a_text_2table_a_desc_2
根据得到的结果可以推测出数据查询的方式:在不使用where条件的前提之下,会以sql中from后面提到的第一张表为基准表,然后和from后面第二张表进行关联的操作,因为sql中没有where,on等关联条件,那么整体的关联方式就是第一张表中的任意一条数据匹配第二张表中的所有数据,假设前表中有N条数据,L1个字段,后表中有M条数据,L2个字段,那么使用上面的sql查询将得到N*M条数据的结果集,结果集的列数为L1+L2。
下面来看一个极端的情况:
假设其中的一张表没有数据,那么将会得到一个空的结果集,仍然满足上面的结论:假设前表中有N条数据,L1个字段,后表中有M条数据,L2个字段,那么使用上面的sql查询将得到N*M条数据的结果集(因为M或者N为0,那么M*N=0),结果集的列数为L1+L2。假设我们使用3张表进行查询,使用下面的sql语句:
select * from table_a,table_b,table_c ,得到的结论仍然一致,假设a表中有M条数据,b表中有N条数据,c表中有K条数据,将得到一个数量为M*N*K的结果集。
2:select * from table_a,table_b where 条件 //使用where条件的前提下
假设table_a的数据如下:
test_idtest_nametest_info
1111222
2111222
假设table_b的数据如下:
test_idtest_nametest_info
1111222
2333222
使用以下的sql进行查询操作: select * from table_a,table_b where table_a.test_name =table_b.test_name,得到的结果集如下操作:
test_idtest_nametest_info
1111111
2444222
将上面的sql换为: select * from table_a inner join table_b on table_a.test_name = table_b.test_name,得到的结果集同上。
现在我们在加入一张table_c表,数据如下:
test_idtest_nametest_infotest_id(1)test_name(1)test_info(1)
11111111111111
分别使用下面的sql语句进行查询:

(1):select * from table_a,table_b,table_c where table_a.test_name = table_b.test_name and table_b.test_name =table_c.test_name

(2):select * from table_a inner join table_b on table_a.test_name = table_b.test_name inner join table_c on table_b.test_name = table_c.test_name
得到的结果集完全相同,也就是是说select * from 多张表 where 条件 和多表联查中的内查询是一样的。
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值