tanlover
Steven.Tan
2010-02-24 18:21
浏览 491

SQL语句中的判断语句

有下面一个SQL:
select * from table_a a_
left outer join table_b b_ on a_.ins_id = b_.id
where (b_.state="ok" or b_.state="error") and b_.date>now()
问题是:
当b_.state="ok"时 b_.date>now()
当b_.state="error"时b_.date>a_.date

这些条件能不能在一个SQL中实现!

数据库为MySql 5.1

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dmewy
    dmewy 2010-02-26 13:46
    已采纳

    where (b_.state="ok" and b_.date>now() ) or ( b_.state="error" and b_.date>a_.date )

    点赞 评论
  • jacki6
    jacki6 2010-02-24 21:22

    做一个子查询 然后用case 试试

    点赞 评论
  • xxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxx 2010-02-26 11:25

    select * from table_a a_
    left join table_b b_ on a_.ins_id = b_.id
    having (b_.state="ok" and b_.date>now() ) or (b_.state="error" and b_.date>a_.date);

    点赞 评论

相关推荐