求一条sql语句

蜗牛彪 2013-07-09 05:54:31
现在有4个表,表结构都很简单,
id name, 都是这样的结构,现在分别知道四个表中的name,想用一条语句把对应的4个id查出来就行,知道的说下,谢谢各位了
...全文
482 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
playclscl 2013-10-12
  • 打赏
  • 举报
回复
以上都错误 你知道所有表中的name 举例 知道name 值是 a b c d 应该这么写看好咯 select t.* from 表1 t where t.name in ('a','b','c','d') union all select t2.* from 表2 t2 where t2.name in ('a','b','c','d') union all select t3.* from 表3 t3 where t3.name in ('a','b','c','d') union all select t4.* from 表4 t4 where t4.name in ('a','b','c','d') 上面查出的做为一张表 再group by 一下看看多少个重复 需要取哪些数据 select count(1) as 数量 ,t5.id , t5.name from ( select t.* from 表1 t where t.name in ('a','b','c','d') union all select t2.* from 表2 t2 where t2.name in ('a','b','c','d') union all select t3.* from 表3 t3 where t3.name in ('a','b','c','d') union all select t4.* from 表4 t4 where t4.name in ('a','b','c','d') ) group by t5.id , t5.name
北极海hein 2013-09-17
  • 打赏
  • 举报
回复
SELECT (select id from table1) name1, (select id from table2) name2, (select id from table3) name3, (select id from table4) name4 from dual;
无敌小二傻 2013-09-09
  • 打赏
  • 举报
回复
引用 5 楼 jbiao5201314 的回复:
union是可以都查出来,但是查出来好像是四列,我希望能查出来是四行,因为这个表结构一样,但是字段名称是不一样的,不知道怎么处理
为什么不用列转行??
duqiu155 2013-09-09
  • 打赏
  • 举报
回复
不错额。。
u010559983 2013-07-26
  • 打赏
  • 举报
回复
楼主是想四个ID 平行显示吧,就是每一行有四个表的ID ,然后显示所有?这样看看可以不? SELECT (select id from table1) name1, (select id from table2) name2, (select id from table3) name3, (select id from table4) name4 from dual;
newjq 2013-07-25
  • 打赏
  • 举报
回复
问非所答!!
rabitsky 2013-07-19
  • 打赏
  • 举报
回复
真尼玛懒的出奇,连个测试数据也不提供
java641 2013-07-18
  • 打赏
  • 举报
回复
楼上的方法 ,要的是 中间3个查询结果集只有一条数据
蜗牛彪 2013-07-10
  • 打赏
  • 举报
回复
搞定了,这样就可以了 SELECT (select name1 from table1 where id1 = '1') name1, (select name2 from table2 where id2 = '2') name2, (select name3 from table3 where id3 = '3') name3 from dual;
shy315 2013-07-10
  • 打赏
  • 举报
回复
引用 2 楼 sych888 的回复:
select id from tab1 where name='name1' union all select id from tab2 where name='name2' union all select id from tab3 where name='name3' union all select id from tab4 where name='name4' ;
四列???
蜗牛彪 2013-07-10
  • 打赏
  • 举报
回复
union是可以都查出来,但是查出来好像是四列,我希望能查出来是四行,因为这个表结构一样,但是字段名称是不一样的,不知道怎么处理
白脑哥 2013-07-09
  • 打赏
  • 举报
回复
楼上的都说了用union all,用并集就可以 如果要去重的话,用union 不去重用union all
善若止水 2013-07-09
  • 打赏
  • 举报
回复
对呀,这是union all的典型用法
sych888 2013-07-09
  • 打赏
  • 举报
回复
select id from tab1 where name='name1' union all select id from tab2 where name='name2' union all select id from tab3 where name='name3' union all select id from tab4 where name='name4' ;
gao_allen 2013-07-09
  • 打赏
  • 举报
回复
用union all

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧