"、"!="、"not in"、"exsits"和"not exists"的使用规范">

"<>"、"!="、"not in"、"exsits"和"not exists"的使用规范

1、原则上一般禁止使用"<>"、"!="和"not in",而应该转换成相应的"="和"in"查询条件
  错误的写法:
  select a.id,a.subject,a.create_type
  from product
  where status <> 'new'
    and owner_member_id = :1

  正确的写法:
  select a.id,a.subject,a.create_type
  from product
  where status in ('auditing','modified','service-delete','tbd','user-delete','wait-for-audit')
    and owner_member_id = :1

  错误的写法:
  select a.id,a.subject,a.create_type
  from product
  where create_type not in ('new_order','vip_add')
    and owner_member_id = :1

  正确的写法:
  select a.id,a.subject,a.create_type
  from product
  where create_type = 'cust_add'
    and owner_member_id = :1

  2、原则上不允许使用"exsits"和"not exists"查询,应转换成相应的"等连接"和外连接来查询
  错误的写法:
  select a.id
  from company a
    where not exsits (select 1
                    from av_info_new b
                    where a.id = b.company_id
  )

  正确的写法:
  select a.id
  from company a,av_info_draft b
  where a.id = b.company_id(+)
    and b.company_id is null

  错误的写法:
  select count
  from company a
  where exsits (select 1
                    from av_info_new b
                    where a.id = b.company_id
  )

  正确的写法:
  select count
  from company a,av_info_draft b
  where a.id = b.company_id

  注:在通过等连接替换exsits的时候有一点需要注意,只有在一对一的时候两者才能较容易替换,如果是一对多的关系,直接替换后两者的结果会出现不一致情况。因为exsits是实现是否存在,他不care存在一条还是多条,而等连接时返回所关联上的所有数据。

  3、如有特殊需要无法完成相应的转换,必须在DBA允许的情况下使用"<>"、"!="、"not in"、"exsits"和"not exists"


你可能感兴趣的:("<>"、"!="、"not in"、"exsits"和"not exists"的使用规范)