求教大神,选出t1表a列和t2表a相同的数据的sql语句,我这样写对不对?

求教大神,选出t1表a列和t2表a相同的数据的sql语句,我这样写对不对?
select t1.a from t1,t2 where t1.a=t2.a,这样写好像不对.假如t1表a列有1个2,t2表a列有3个2,就会选出3个2来.我的本意是选出1个2. 求教一个简捷的方法
shadow小混 1年前 已收到1个回答 举报

ruohan2003 幼苗

共回答了22个问题采纳率:95.5% 举报

select distinct t1.a from t1,t2 where t1.a=t2.a

1年前 追问

9

shadow小混 举报

这样不对,如果t1.a中有重复的数据,我只选了一条。

举报 ruohan2003

select t1.a from t1 where exists(select 1 from t2 where t1.a=t2.a);
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.027 s. - webmaster@yulucn.com