问题是这样的: SQL_Sever数据库有好几张表,其中用户表:users_table中的id字段包含所有用户的编号,其他表中有部分用户编号id,我想查询的数据是这样的: select * from user_table where id存在于其他所有表中,(即id是的其他表的合集) 谢谢各位的帮助,由于这个数据库非常庞大,需要一种执行比较快的SQL语句,或其他方法. 请各位继续加油,如果被采纳悬赏分直接送出.
首先你这个users_table 的ID字段最好设置成主键,其次其他所有表里的ID都需要有索引,这样才能保证速度。 select * from user_table a where exists (select 1 from sub_table1 b where b.id = a.id ) or exists (select 1 from sub_table2 c where c.id = a.id ) ......
第2个回答 2007-12-29
你应该这样: select *from users_table a,users_table1 b,users_table2 c where a.id=b.id=c.id 其中users_table1、users_table2是你的其他表部分 你还在吗?问题解决没有? 既然遇到问题怎么不回我的话?这样我怎么帮你解决呢?朋友!
第3个回答 2008-01-05
创建视图应该是最快的: create view vtest as select id from 其他表1 union select id from 其他表2 ... go --查询 select * from user_table a inner join vtest b on a.id=b.id go本回答被提问者采纳