一个表 a (id int, a int, b int )
例如
id a b
1 1 3
2 1 4
3 1 5
4 2 1
5 2 3
6 3 3
7 3 3
8 4 3
9 5 2
。。这里的限制条件是group by a, b
如果 在 a b 的各个分组里 b全部为3 那么这个分组去掉。
按照这个条件上面的记录 id=5,6,7,8 的将去掉 (a b 分组b不全为3的可以保留)急切询问这个语句如何写
例如
id a b
1 1 3
2 1 4
3 1 5
4 2 1
5 2 3
6 3 3
7 3 3
8 4 3
9 5 2
。。这里的限制条件是group by a, b
如果 在 a b 的各个分组里 b全部为3 那么这个分组去掉。
按照这个条件上面的记录 id=5,6,7,8 的将去掉 (a b 分组b不全为3的可以保留)急切询问这个语句如何写
解决方案 »
- MySQL命令栏打不开
- sql语句中select怎样查找最大字段的结果呢
- 数据库语句,大家都进来看看啊,我是菜鸟.....
- mysql 数据延时问题
- postgresql 问题 call 存储过程 急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 这样一个sql语句,该如何优化呢?谢谢~~~解决立刻给分
- ecpg是不是Postgresql自带的预编译器
- mysql的连接问题!
- 有个SQL语句不会写了,那位大侠能否帮忙
- Mysql能不能使用NAS存储映射驱动器存储数据?
- 导入utf8数据文件到数据库出错
- 导出再倒入,多了一条数据(字段),ACMAIN_CHM,wwwwb请进接分
5 2 3
6 3 3
7 3 3
8 4 3能解释一下,为什么 8 也去掉?
这个分组 里 ,a=4, b=3
而且 仅仅该组只有1条记录 b=3
所以去掉
select * from ..
。group by a,b 按照a b 进行分组如果在 a b 一个分组里b的值全部为3 那么这个分组去掉
直接在分组前加个 where b!=3,是这样吗?
当分组a《》3 数据全部保留
5 2 3
这个分组 a=2 , b=1,和 3 ,这个a=2的分组里b不全部为3 所以 该组保留请注意现在难度大大减低:
现在简化sql (难度大大降低) 当分组 a=3 的时候 如果该组 b的值全部为3那么这个分组去掉。 当分组 a=3 的时候 如果该组 b的值不全部为3那么这个分组保留 当分组a《》3 数据全部保留
当分组 a=3 的时候 如果该组 b的值全部为3那么这个分组去掉。 当分组 a=3 的时候 如果该组 b的值不全部为3那么这个分组保留 当分组a《》3 数据全部保留mysql> select *
-> from t_liyihongcug t
-> where a!=3
-> or exists (select 1 from t_liyihongcug where a=t.a and b!=3);
+------+------+------+
| id | a | b |
+------+------+------+
| 1 | 1 | 3 |
| 2 | 1 | 4 |
| 3 | 1 | 5 |
| 4 | 2 | 1 |
| 5 | 2 | 3 |
| 8 | 4 | 3 |
| 9 | 5 | 2 |
+------+------+------+
7 rows in set (0.00 sec)mysql>
-> from t_liyihongcug t
-> where a!=3
-> or exists (select 1 from t_liyihongcug where a=t.a and b!=3);感觉有问题的
exists (select 1 from t_liyihongcug where a=t.a and b!=3);
(如果 有
a=3,b=3
a=3 ,b=4)那么a=3 这个分组的所有数据是应该保留的哦。 ???
但是select *
-> from t_liyihongcug t
我实际的不是一个表的 实际上是多个表组合而成的哦
select * from (select b.disid , a. typeId from Out a, Event b
where a.eventId=b.id) t
where
t.disid != 3 or
exist (select 1 from t where t.disciplineid=3 and t.typeId!=47);死活编译不通过 ,可能逻辑上有问题 ??
如果他是多个表, 为了简化,现在肯定他是2个表组合而成的哦
如 (select * from b, c where b.id=c.id ) t 不知道如何改写这个sql
2 另外表很不方便公开
学会举例。你可以把表名改成 t1,t2,t3可以把字段名改成 c1,c2,c3 ,可以把数据改成 AA,BB 1,2,3你希望我怎么自己搭测试环境呢?宁肯让别人乱猜,你就不肯提供一下?
where a.eventId=b.id) t
where
t.disid != 3 or
exist (select 1 from (select b.disid , a. typeId from Out a, Event b
where a.eventId=b.id) m where m.eventId=t.eventId and t.disid=3 and t.typeId!=3);
就是执行速度过慢 , 几乎失去了查询的意义, 过慢!
select b.* from b, c where b.id=c.id 这样形成一个表(这个表就是你的t_liyihongcug)之后把这个形成的表如何融入到你的sql中
select *
-> from t_liyihongcug t
-> where a!=3
-> or exists (select 1 from t_liyihongcug where a=t.a and b!=3);
select *
-> from t_liyihongcug t
-> where a!=3
-> or exists (select 1 from t_liyihongcug where a=t.a and b!=3); 估计有点慢????
我的解决方法 select * from (select b.disid ,a.eventid, a. typeId from Out a, Event b
where a.eventId=b.id) t
where
t.disid != 3 or
exist (select 1 from (select b.disid , a. typeId from Out a, Event b
where a.eventId=b.id) m where m.eventId=t.eventId and t.disid=3 and t.typeId!=3);
执行太慢 几乎10分钟不能数据 已经失去了查询意义
2。我自己编。你希望是哪一种呢?