id address contentid status
5 0 14 0
6 22 15 1
7 22 14 1
8 0 16 0求一条sql 查询出如果有contentid 相等的 就只查出address不等于0的
如 contentid 有2条等于14就不查出id等于5的那条。像id等于8的那条address等于0 没有与它相等的就查出等于0的。
5 0 14 0
6 22 15 1
7 22 14 1
8 0 16 0求一条sql 查询出如果有contentid 相等的 就只查出address不等于0的
如 contentid 有2条等于14就不查出id等于5的那条。像id等于8的那条address等于0 没有与它相等的就查出等于0的。
1、select contentid,count(contentid) from tablename group by contentid
2、select * from tablename where address!=0 and contentid = 第一条查询出来的contentid
( status=0 AND NOT EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND status=1 ))
OR
( status=1 AND NOT EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND A.status=0 ))
SELECT * FROM TTY A WHERE
( status=1 AND EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND status=0 ))
or
( status=1 AND EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND status=1 ))
or
( status=0 AND NOT EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND status=1 ))
SELECT * FROM TTY A WHERE
( status=1 AND EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND status=0 ))
or
( status=1 AND EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND status=1 ))
or
( status=0 AND NOT EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND status=1 ))
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
5 0 14 0
6 22 15 1
7 22 14 1
8 0 16 0
9 22 16 0
要的结果是查询出id为6,7,8,9的
create table `user_messageinfo` (
`id` double ,
`messagetype` double ,
`templateid` double ,
`addressee` double ,
`contentid` double ,
`fromid` varchar (150),
`TYPE` double ,
`STATUS` double
);
insert into `user_messageinfo` (`id`, `messagetype`, `templateid`, `addressee`, `contentid`, `fromid`, `TYPE`, `STATUS`) values('5','1',NULL,'0','14',NULL,'1','0');
insert into `user_messageinfo` (`id`, `messagetype`, `templateid`, `addressee`, `contentid`, `fromid`, `TYPE`, `STATUS`) values('6','1',NULL,'22','15',NULL,'1','1');
insert into `user_messageinfo` (`id`, `messagetype`, `templateid`, `addressee`, `contentid`, `fromid`, `TYPE`, `STATUS`) values('15','1',NULL,'22','14',NULL,'2','1');
insert into `user_messageinfo` (`id`, `messagetype`, `templateid`, `addressee`, `contentid`, `fromid`, `TYPE`, `STATUS`) values('16','1',NULL,'0','16',NULL,'1','0');
insert into `user_messageinfo` (`id`, `messagetype`, `templateid`, `addressee`, `contentid`, `fromid`, `TYPE`, `STATUS`) values('17','1',NULL,'22','16',NULL,'2','0');
查出id为6 15 17的数据,如果有contentid有相等的就过滤掉address为0的那条。没有就查出address为0的那条。
数据库为mysql5.0
因为8 9的contentid一样。显示address不为0的那条。
SELECT * FROM `user_messageinfo` a WHERE
( `addressee`<>0 AND EXISTS(SELECT 1 FROM `user_messageinfo`
WHERE a.`contentid`=`contentid` ))
NOT EXISTS(SELECT 1 FROM `user_messageinfo` WHERE a.`contentid`=a.`contentid` AND a.`addressee`=0)
比如9不存在就显示8
NOT EXISTS(SELECT 1 FROM tty WHERE a.contentid=a.contentid AND a.address=0)
ornot EXISTS(SELECT 1 FROM tty WHERE a.contentid=a.contentid AND a.address=0 and a.id<id)
SELECT 1 FROM tty WHERE a.contentid=a.contentid AND a.address=0 and a.id<id
这一句我的理解是address=0的那条的id小于必须小于address 不等于0的那条的id,如果大于的话就满足不了了。?
id address contentid status
5 0 14 0
6 22 15 1
7 22 14 1
8 0 16 0
9 22 16 0
NOT EXISTS(SELECT 1 FROM tty WHERE a.contentid=contentid AND a.address=0)
or
not EXISTS(SELECT 1 FROM tty WHERE a.contentid=contentid AND a.address=0 and a.id<id)不是,你可以分开测试
SELECT * FROM tty a WHERE
NOT EXISTS(SELECT 1 FROM tty WHERE a.contentid=contentid AND a.address=0)SELECT * FROM tty a WHERE
not EXISTS(SELECT 1 FROM tty WHERE a.contentid=contentid AND a.address=0 and a.id<id)看看结果
现在有个新问题。 刚刚没提到。
id address contentid status
5 0 14 0
6 22 15 1
7 22 14 1
8 0 16 0
9 22 16 0
刚才条件的基础之上还要加上status
如果contentid有相等的并且status都为0就不查出来
如果contentid没有相等的就查询出
如果contentid有相等的且id大的那条等于1 就查询出id大的那条
如上。
查询出id等于6,7
id等于8,9的就不查出来了。
select * from tt where status='1'
SELECT * FROM TTY A WHERE
(A.status=1 AND NOT EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND A.ID<ID ))
OR
(NOT EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid AND A.ID<>ID))
OR
(A.status=0 AND NOT EXISTS(SELECT 1 FROM TTY WHERE A.contentid=contentid))