A表字段如下:
IDA,TimeA,Ifbit
B表字段如下:
IDB,IDA,TimeB
很简单的模拟,其中IDB可以对应多个IDA,现在要求IDA和IDB按条件找出来,
1。首先把表A中Ifbit=1,对应最大的TimeB大于三个月的的IDA找出来
2。找出这些IDA之后,在根据这些IDA找出对应的唯一的IDB,
3,如果这些找出的IDB对应的所有的IDA都满足条件1,就找出这些IDB,
以上就是条件要找的IDA和IDB,请问如何使用SQL语句找出
比如表A
IDA TimeA Ifbit
1 2010-2-3 1
2 2010-3-6 0
3 2010-3-11 0
4 2010-3-19 1
表B的数据如下
IDB IDA TimeB
1 1 2010-4-5
1 2 2010-4-6
2 2 2010-4-7
2 3 2010-4-8
3 4 2010-4-10
首先要找表A中Ifbit=1,对应的TimeB和当前时间先比大3个月的IDA,这个时候一看就是到IDA=1,IDA=4符合,这样我们就把IDA找出来了,然后我们看到根据IDA=2,IDA=3对应找到IDB=1,IDB=3,但是IDB=1关联的是IDA=1,IDA=2,而IDA=2的Ifbit=0,不符合,所以IDB=1不符合,而IDB=3符合,所有最后我们找到的IDA=1,IDA=4和IDB=3,求SQL语句
貌似你描述的过程有点前后矛盾:
1。首先要找表A中Ifbit=1,对应的TimeB和当前时间先比大3个月的IDA,这个时候一看就是到IDA=1,IDA=4符合
TimeB是不是TimeA?
2。然后我们看到根据IDA=2,IDA=3对应找到IDB=1,IDB=3,如果通过B表的IDA和A表的IDA连接来看
IDB=3对应的记录是A表中的IDA=4的记录。
--> 测试数据: [a]
if object_id('[a]') is not null drop table [a]
create table [a] (IDA int,TimeA datetime,Ifbit int)
insert into [a]
select 1,'2010-2-3',1 union all
select 2,'2010-3-6',0 union all
select 3,'2010-3-11',0 union all
select 4,'2010-3-19',1
--> 测试数据: [b]
if object_id('[b]') is not null drop table [b]
create table [b] (IDB int,IDA int,TimeB datetime)
insert into [b]
select 1,1,'2010-4-5' union all
select 1,2,'2010-4-6' union all
select 2,2,'2010-4-7' union all
select 2,3,'2010-4-8' union all
select 3,4,'2010-4-10'
go
--查ida
select ida from a t,b where t.ida=b.ida and t.ifbit=1 and datediff(mm,b.timeb,getdate())>3
--查idb
select idb from
(select c.ida,b.idb from a t,b,b c where t.ida=b.ida and t.ifbit=1 and datediff(mm,b.timeb,getdate())>3 and b.idb=c.idb)m
where not exists(select idb from a,b where a.ida=b.ida and idb=m.idb and ifbit=0)
(SELECT A.IDA AS ID,'IDA' AS 字段名 FROM A INNER JOIN B ON A.IDA=B.IDA WHERE DATEDIFF(MONTH,GETDATE(),B.TIMEB)>3 AND A.Ifbit=1)
union
(
SELECT B.IDB AS ID,'IDB' AS 字段名 FROM B WHERE NOT EXISTS (SELECT B.IDB FROM A INNER JOIN B ON A.IDA=B.IDA WHERE A.Ifbit=0 OR DATEDIFF(MONTH,GETDATE(),B.TIMEB)<3)
)
CREATE TABLE #A
(
IDA int,
TimeA date,
Ifbit bit
)
CREATE TABLE #B
(
IDB int,
IDA int,
TimeB date
)
INSERT INTO #A(IDA,TimeA,Ifbit)VALUES(1 ,'2010-2-3', 1)
INSERT INTO #A(IDA,TimeA,Ifbit)VALUES(2 ,'2010-3-6' ,0)
INSERT INTO #A(IDA,TimeA,Ifbit)VALUES(3, '2010-3-11' ,0)
INSERT INTO #A(IDA,TimeA,Ifbit)VALUES(4 ,'2010-3-19', 1)
INSERT INTO #B(IDB,IDA,TimeB)VALUES(1 ,1,'2010-4-5')
INSERT INTO #B(IDB,IDA,TimeB)VALUES(1 ,2 ,'2010-4-6')
INSERT INTO #B(IDB,IDA,TimeB)VALUES(2 ,2, '2010-4-7')
INSERT INTO #B(IDB,IDA,TimeB)VALUES(2, 3, '2010-4-8')
INSERT INTO #B(IDB,IDA,TimeB)VALUES(3 ,4 ,'2010-4-10')
INSERT INTO #B(IDB,IDA,TimeB)VALUES(1 ,3 ,'2010-4-10')
--1。首先把表A中Ifbit=1,对应最大的TimeB大于三个月的的IDA找出来
--不知道对应最大的TimeB大于三个月的意思是不是说TimeB的月份大于3月,
--我对你的意思不太清楚
SELECT A.IDA,B.IDB
FROM
(
--step1
SELECT a.IDA,b.IDB
FROM #A a
INNER JOIN #B b
ON a.IDA = b.IDA
WHERE 3<datediff(mm,b.TimeB,getdate())
AND a.Ifbit =1
)A LEFT JOIN
(
--step2
SELECT d.IDB
FROM #A c
INNER JOIN #B d
ON c.IDA = d.IDA
WHERE c.Ifbit = 1
GROUP BY d.IDB
HAVING COUNT(*)=(SELECT COUNT(*) FROM #B WHERE IDB = d.IDB)
)B
ON A.IDB = B.IDB
你可以复制整个语句到sql中执行,结果就出来了。
即使跟你要的结果不一样,也可领会其中一些知识