有一数据表A,结构如下
ID RID
1 1
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 4
4 1
4 2
4 3
4 4问题是:我如何取出RID即等于1又等于3的不重复ID?即结果是:1,2,4
除了用intersect这个,还有其他方法吗?
ID RID
1 1
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 4
4 1
4 2
4 3
4 4问题是:我如何取出RID即等于1又等于3的不重复ID?即结果是:1,2,4
除了用intersect这个,还有其他方法吗?
解决方案 »
- 一个有关字段内含有下划线查询的SQL语句问题
- 在线等:查询排序语句理解!
- 【急急】数据库存放歌曲和图片问题
- 请问大家,我的SQLSERVER数据库为什么服务器不能启动了!
- 请教高手!急!!!!!!!!!!!
- 不懂,我在定义完数据库后我的技术主管让我写自定义函数,但我不知道这些有什么用,我以前都是用存储过程的.
- 数据筛选出需要的数据
- 大家帮忙看看是否是sqlserver得问题! sqlserver服务器运行 后,不到半天 有些客户端就无法连接到服务器得sqlserver!服务器内存占用很多....
- 关于 timestamp
- 请问哪里有MCDBA的免费的电子教材及考试模拟题下载?
- SQL2005备份 还原到SQL2000越到的问题
- 急求答案,高手请赐教啊.
and exists(select 1 from A where id=t.id and RID=3)
drop table tb
Go
Create table tb([ID] int,[RID] int)
Insert tb
select 1,1 union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 2,1 union all
select 2,3 union all
select 2,4 union all
select 3,1 union all
select 3,4 union all
select 4,1 union all
select 4,2 union all
select 4,3 union all
select 4,4
GO
SELECT ID
FROM ( SELECT *
FROM dbo.tb PIVOT( COUNT(RID) FOR rid IN ( [1], [3] ) )AS p
) AS T
WHERE [1] = 1
AND [3] = 1
(select id,
RID=(select ''+rtrim(id) from A where id=t.id)
from A t group by id) t
where patindex('%[^1234]%',rid)=0
id
from
tb
where
RID in(1,3)
and
ID in(select ID from tb group by ID having COUNT(1)>2)
group by
ID
if not object_id('tb') is null
drop table tb
Go
Create table tb([ID] int,[RID] int)
Insert tb
select 1,1 union all
select 1,2 union all
select 1,3 union all
select 1,4 union all
select 2,1 union all
select 2,3 union all
select 2,4 union all
select 3,1 union all
select 3,4 union all
select 4,1 union all
select 4,2 union all
select 4,3 union all
select 4,4
GO
--这个写法适用于RID不重复的情况
SELECT ID
FROM TB
WHERE RID IN (1,3) --写你的条件
GROUP BY ID HAVING COUNT(1)=2 --有几个数就写几
/*
1
2
4
*/
本意是这样的:这是一张酒店的资料表,酒店的基础信息我存在一张表里,这张表存储的是酒店的配套设施,比如网线,空调,电视等等,和酒店的房间属于一对多的关系,所以我存了分表,先要前台要查询有网线并且有空调的酒店房间!我想问一下,我这样设计的表结构有问题吗?有更好的解决方案吗?
如果不固定,也可以试试用枚举值放在一列存储,比如,1,2,这样的,查找的时候用LIKE叠加条件即可。
如果不固定,也可以试试用枚举值放在一列存储,比如,1,2,这样的,查找的时候用LIKE叠加条件即可。不是固定的,但如果按照枚举值放在一列存储,就要写很多LIKE,我担心速度会慢,比如:
select ID from A where RID LIKE '%,1,%' AND RID LIKE '%,2,%' AND RID LIKE '%,3,%' AND RID LIKE '%,4,%'......,速度难以保障