V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
corboy
V2EX  ›  PHP

这两条 sql 语句可否合并为一条?

  •  
  •   corboy · 2015-11-17 16:01:52 +08:00 · 2730 次点击
    这是一个创建于 3326 天前的主题,其中的信息可能已经有所发展或是发生改变。

    SELECT * FROM comments where postId = 92057 AND auditBy IS NOT NULL;
    SELECT * FROM comments where postId = 92057 AND auditBy IS NULL AND userId = 1111;

    qsl0913
        1
    qsl0913  
       2015-11-17 16:06:50 +08:00
    SELECT * FROM comments where postId = 92057 AND (auditBy IS NOT NULL or (auditBy IS NULL AND userId = 1111));
    corboy
        2
    corboy  
    OP
       2015-11-17 16:06:57 +08:00
    自己已解决,谢谢。
    barbery
        3
    barbery  
       2015-11-17 16:07:02 +08:00
    要么条件合并: SELECT * FROM comments where postId = 92057 AND auditBy IS NOT NULL OR (auditBy IS NULL AND userId = 1111)
    要么链接来执行: SELECT * FROM comments where postId = 92057 AND auditBy IS NOT NULL UNION ALL SELECT * FROM comments where postId = 92057 AND auditBy IS NULL AND userId = 1111;
    lingdong
        4
    lingdong  
       2015-11-17 16:07:06 +08:00
    SELECT * FROM comments where postId = 92057 AND auditBy IS NOT NULL OR (auditBy IS NULL AND userId = 1111);
    这样 OK 吗?
    ixiaozhi
        5
    ixiaozhi  
       2015-11-17 16:07:14 +08:00
    SELECT * FROM comments where postId = 92057 AND ( auditBy IS NOT NULL ) OR ( auditBy IS NULL AND userId = 1111)
    corboy
        6
    corboy  
    OP
       2015-11-17 16:07:16 +08:00
    @qsl0913 thanks
    123123
        7
    123123  
       2015-11-17 19:23:14 +08:00   ❤️ 1
    @barbery @lingdong @ixiaozhi
    有其他条件的情况下, OR 条件不加上括号怎么行?
    看三层都这么写也是醉了
    barbery
        8
    barbery  
       2015-11-17 22:59:31 +08:00
    @123123 谢谢指出问题,已感谢
    TangMonk
        9
    TangMonk  
       2015-11-18 10:27:49 +08:00
    UNION
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5641 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 03:32 · PVG 11:32 · LAX 19:32 · JFK 22:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.