Mysql exists用法小结

  # 查询体重秤

  select * from activity_main where act_code in (

  select act_code from activity_sku where sku = '翎野君的体脂称'

  )

  # 查询体重秤

  select * from activity_main a where exists (

  select 1 from activity_sku b where a.act_code = b.act_code and b.sku = '翎野君的体脂称'

  )

  # 模糊查询B-BEKO英国婴儿推车

  select * from activity_main where act_code in (

  select act_code from activity_sku where sku like '%B-BEKO%'

  )

  # 模糊查询B-BEKO英国婴儿推车

  select * from activity_main a where exists (

  select 1 from activity_sku b where a.act_code = b.act_code and b.sku like '%B-BEKO%'

  )

  # 查询在博客园举办的活动

  select * from activity_main where act_code in (

  select act_code from activity_area where area = '博客园'

  )

  # 查询在博客园举办的活动

  select * from activity_main a where exists (

  select 1 from activity_area b where a.act_code = b.act_code and b.area = '博客园'

  )

  # 在博客园举办活动且活动奖品为华为手机的活动信息

  select * from activity_main where act_code in (

  select act_code from activity_area where area = '博客园' and act_code in (

  select act_code from activity_sku where sku = '华为P30Pro'

  ))

  # 内层的exists语句只在当前where语句中生效,最终是否返回,要根据最外层的exists判断,如果是 true(真)就返回到结果集,为 false(假)丢弃。

  select * from activity_main a where exists (

  select 1 from activity_area b where a.act_code = b.act_code and b.area = '博客园' and exists

  (select 1 from activity_sku c where a.act_code = c.act_code and c.sku = '华为P30Pro')

  )