sql第一次筛选后将其排除,又进行二次筛选,如何实现?
答案:2 悬赏:80 手机版
解决时间 2021-03-13 22:59
- 提问者网友:呐年旧曙光
- 2021-03-13 11:29
sql第一次筛选后将其排除,又进行二次筛选,如何实现?
最佳答案
- 五星知识达人网友:行路难
- 2021-03-13 12:04
用cte的写法
with t as (select top 1 * from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' and zhiding=true order by add_time DESC,id desc)
select * from book a where (ntype=0 or ntype=1 or ntype=2) and revert<>''
and not exists(select 1 from t where t.主键=a.主键) ) order by add_time DESC,id desc
with t as (select top 1 * from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' and zhiding=true order by add_time DESC,id desc)
select * from book a where (ntype=0 or ntype=1 or ntype=2) and revert<>''
and not exists(select 1 from t where t.主键=a.主键) ) order by add_time DESC,id desc
全部回答
- 1楼网友:持酒劝斜阳
- 2021-03-13 13:33
select * from
(
select top 1 *,0 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' and zhiding=true order by add_time DESC,id desc
union
select * ,1 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' order by add_time DESC,id desc
) aaa
order by id_new,add_time DESC,id desc追问
请问大师,能否解释一下?,0 as id_new和1 as id_new这个是什么意思?aaa又是指什么?
追答select * from
(
select top 1 *,0 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' and zhiding=true
union
select * ,1 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>''
) aaa
order by id_new,add_time DESC,id desc
,0 as id_new和1 as id_new这个是手动新增出来的2列,用于排序的,aaa是两个查询联合后的表的别名追问老师,经过测试发现一个问题,第一条至顶能起作用,但后面几条也包含了至顶的那一条,没有将其排除。
(
select top 1 *,0 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' and zhiding=true order by add_time DESC,id desc
union
select * ,1 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' order by add_time DESC,id desc
) aaa
order by id_new,add_time DESC,id desc追问
请问大师,能否解释一下?,0 as id_new和1 as id_new这个是什么意思?aaa又是指什么?
追答select * from
(
select top 1 *,0 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>'' and zhiding=true
union
select * ,1 as id_new from book where (ntype=0 or ntype=1 or ntype=2) and revert<>''
) aaa
order by id_new,add_time DESC,id desc
,0 as id_new和1 as id_new这个是手动新增出来的2列,用于排序的,aaa是两个查询联合后的表的别名追问老师,经过测试发现一个问题,第一条至顶能起作用,但后面几条也包含了至顶的那一条,没有将其排除。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯