表结构大致如下:
Table_A:
A_id A_content A_others
1 xxx ……
2 xxx ……
3 xxx ……
4 xxx ……
Table_B:
B_id B_content B_others A_ids
1 …… …… 1,2
2 …… …… 1,2,3
3 …… …… 2,3
也就是说Table_B中的A_ids存放的是Table_A的ID
怎样查询出A_ids对应Table_A的记录?
select * from Table_A A where A.A_id in(1,2,3)
select B.A_ids from Table_B B where B.B_id = 2 --输出为:1,2,3select * from Table_A A where A.A_id in(select B.A_ids from Table_B B where B.B_id)
这样写是不行的!奇怪!
Table_A:
A_id A_content A_others
1 xxx ……
2 xxx ……
3 xxx ……
4 xxx ……
Table_B:
B_id B_content B_others A_ids
1 …… …… 1,2
2 …… …… 1,2,3
3 …… …… 2,3
也就是说Table_B中的A_ids存放的是Table_A的ID
怎样查询出A_ids对应Table_A的记录?
select * from Table_A A where A.A_id in(1,2,3)
select B.A_ids from Table_B B where B.B_id = 2 --输出为:1,2,3select * from Table_A A where A.A_id in(select B.A_ids from Table_B B where B.B_id)
这样写是不行的!奇怪!
解决方案 »
- pb如何调用这个存储过程,请大虾们帮我~!
- microsoft sql 企业管理器 建表时出现错误:mmc检测到此管理单元发生一个错误 建议您关闭并重新启动mmc
- 问大家个聚合函数求和的问题。分不多。诚心受教!
- 关于 SQL Server 用户操作界面 问题
- sql里怎么获得类似vb.net 的 GetHashCode 哈希值?我需要是整型的值
- 输出两个参数的存储过程如何调用这两个参数呢??
- 存储过程导出问题,请高手
- 为什么在windows 2000 professional 上安装 sql sever 2000 个人版总出错
- Sql2005卸载后 重新安装安装 存在同名实例
- vfp中如何进行整除?
- 批量 更新 问题,太慢了,受不了
- 多人开发系统,数据库方面的改动如果管理
insert @t select 1,'A'
insert @t select 2,'B'
declare @s table(ID int,name varchar(10))
insert @s select 1,'A'
insert @s select 2,'B'
insert @s select 1,'B'
select * from @t t where t.ID in(select s.ID from @s s where s.name='B')
ID name
----------- ----------
1 A
2 B
declare @t1 table(id int identity(1,1),name char(1))
declare @t2 table(id int identity(1,1),name char(2),a_id varchar(20))
insert @t1 select 'a'
union all select 'b'
union all select 'c'
union all select 'd'
union all select 'e'insert @t2 select 'aa','1,2'
union all select 'bb','1,2,3'
union all select 'cc','2,3'
union all select 'cc','3,4'select * from @t1
select * from @t2
select distinct
a.*
from @t1 a,@t2 b
where charindex(convert(varchar(10),a.id),b.a_id) > 0
/**
1 a
2 b
3 c
4 d
**/
declare @sql nvarchar(4000)
select @sql=isnull(@sql,'') + N'select * from Table_A where A_id in (' + max(A_ids) + ')'
from Table_B
group by A_ids
exec (@sql)就可以了,哈哈.