急!SQL 1=1 1=2

小孔大胖 2017-09-21 11:49:16
话不多说上代码
insert into temp_sameSiteType_ck(city,distance,sitecode,sitetype) select t.city,t.distance,t.sitecode,t.sitetype from samesitetypestaticdetail t where 1= 1 and trunc(INSERT_TIME) = trunc(to_date('2017.09.20','yyyy-mm-dd')) and (1 = 2 or( t.sitetype = '1111'and t.distance >1111) or( t.sitetype = '2222'and t.distance >2222) or( t.sitetype = '4444'and t.distance >4444) or t.sitetype = '5555'and t.distance >5555 or (t.sitetype = '3333'and t.distance >3333) ) and not exists (select sitename from SITE_SAME_WHITELIST b where b.sitename=t.sitecode)
求大神分析一下代码什么意思..尤其是1=2那个位置 有无编译错误.
...全文
198 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
小孔大胖 2017-09-21
  • 打赏
  • 举报
回复
...能帮忙看一下吗 现在没条件看
OwenZeng_DBA 2017-09-21
  • 打赏
  • 举报
回复
你运行下或者查看下执行计划,就知道了
二月十六 2017-09-21
  • 打赏
  • 举报
回复
引用 7 楼 qq_40109328 的回复:
[quote=引用 6 楼 sinat_28984567 的回复:] 没问题,1=2这个是false,所以会执行or后边的那些条件。如果是1=1的话,or后边那些条件就不走了
所以是什么意思呢 这段代码[/quote] 就是这个意思
INSERT  INTO temp_sameSiteType_ck
        ( city ,
          distance ,
          sitecode ,
          sitetype
        )
        SELECT  t.city ,
                t.distance ,
                t.sitecode ,
                t.sitetype
        FROM    samesitetypestaticdetail t
        WHERE   1 = 1
                AND trunc(INSERT_TIME) = trunc(to_date('2017.09.20',
                                                       'yyyy-mm-dd'))
                AND (  ( t.sitetype = '1111'
                           AND t.distance > 1111
                         )
                      OR ( t.sitetype = '2222'
                           AND t.distance > 2222
                         )
                      OR ( t.sitetype = '4444'
                           AND t.distance > 4444
                         )
                      OR t.sitetype = '5555'
                      AND t.distance > 5555
                      OR ( t.sitetype = '3333'
                           AND t.distance > 3333
                         )
                    )
                AND NOT EXISTS ( SELECT sitename
                                 FROM   SITE_SAME_WHITELIST b
                                 WHERE  b.sitename = t.sitecode )
小孔大胖 2017-09-21
  • 打赏
  • 举报
回复
引用 6 楼 sinat_28984567 的回复:
没问题,1=2这个是false,所以会执行or后边的那些条件。如果是1=1的话,or后边那些条件就不走了
所以是什么意思呢 这段代码
听雨停了 2017-09-21
  • 打赏
  • 举报
回复
引用 5 楼 qq_40109328 的回复:
@qq_37170555 那这段代码是什么意思呢~

--插入表中
INSERT INTO temp_sameSiteType_ck
  (
    city,
    distance,
    sitecode,
    sitetype
  )
--查询数据
SELECT t.city,
       t.distance,
       t.sitecode,
       t.sitetype
FROM   samesitetypestaticdetail t
WHERE  1 = 1
       AND trunc(INSERT_TIME) = trunc(to_date('2017.09.20', 'yyyy-mm-dd'))
       AND (
               1 = 2
               OR (t.sitetype = '1111' AND t.distance > 1111)
               OR (t.sitetype = '2222' AND t.distance > 2222)
               OR (t.sitetype = '4444' AND t.distance > 4444)
               OR t.sitetype = '5555'
               AND t.distance > 5555
               OR (t.sitetype = '3333' AND t.distance > 3333)
           )
       AND NOT EXISTS (
               SELECT sitename
               FROM   SITE_SAME_WHITELIST b
               WHERE  b.sitename = t.sitecode
           )
这段代码格式化一下你就能看明白了,其实就是把查询出来的结果集插入表temp_sameSiteType_ck中。当满足

(
               1 = 2
               OR (t.sitetype = '1111' AND t.distance > 1111)	--1
               OR (t.sitetype = '2222' AND t.distance > 2222)	--2
               OR (t.sitetype = '4444' AND t.distance > 4444)	--3
               OR t.sitetype = '5555'	--4
               AND t.distance > 5555
               OR (t.sitetype = '3333' AND t.distance > 3333)	--5
           )
这个上面五个其中任何一个,且满足 t.distance > 5555的时候,查询就会有数据。这时候就有数据插入表temp_sameSiteType_ck中。相反的如果都不满足这五个or,那么查询就会变成where 1=1 and 1=2。这个时候就不会有数据出来,也就不会有数据插入表temp_sameSiteType_ck中。所以1=2写不写都没多大关系吧。不明白为啥要写1=1和1=2进去,写了跟没写有啥区别啊
二月十六 2017-09-21
  • 打赏
  • 举报
回复
没问题,1=2这个是false,所以会执行or后边的那些条件。如果是1=1的话,or后边那些条件就不走了
小孔大胖 2017-09-21
  • 打赏
  • 举报
回复
@qq_37170555 那这段代码是什么意思呢~
听雨停了 2017-09-21
  • 打赏
  • 举报
回复
不存在编制错误。1=2写在这的意思就是,当(1=2 or .....)里面的or的条件都不满足的时候,条件就会变成1=2,然后前面又写了where 1=1,再加上and 1=2,这个时候就查询不出来数据。其实1=2写不写都无所谓。就算你不写,当查询满足不了(1=2 or...)里面的其他任何一个or条件时,还是查询不出数据。所以写不写都没多大意义。个人理解,仅供参考哈
OwenZeng_DBA 2017-09-21
  • 打赏
  • 举报
回复
引用 2 楼 qq_40109328的回复:
...能帮忙看一下吗 现在没条件看
没有编译错误

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧