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

2015-11-17 16:01:52 +08:00
 corboy

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

2680 次点击
所在节点    PHP
9 条回复
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
2015-11-17 16:06:57 +08:00
自己已解决,谢谢。
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
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
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
2015-11-17 16:07:16 +08:00
@qsl0913 thanks
123123
2015-11-17 19:23:14 +08:00
@barbery @lingdong @ixiaozhi
有其他条件的情况下, OR 条件不加上括号怎么行?
看三层都这么写也是醉了
barbery
2015-11-17 22:59:31 +08:00
@123123 谢谢指出问题,已感谢
TangMonk
2015-11-18 10:27:49 +08:00
UNION

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/236808

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX