首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >取消组查询结果

取消组查询结果
EN

Stack Overflow用户
提问于 2017-08-12 22:52:27
回答 1查看 521关注 0票数 1

如果两个请求都返回相同的数据,那么它们将被“分组”到联合中。

代码语言:javascript
复制
query1 return X 
query2 return X

query1 union query2 renurn only X

But i need to get
+---+
| X |
| X |
+---+

如果每个请求返回不相等的数据,则union的结果将是consist of two lines. This is normal

代码语言:javascript
复制
mysql> select count(pid) from posts 
where uid_posts=8890 and postacc=1 and postcomacc=1 
union
select count(comid) from coms join posts on pid_coms=pid 
where uid_posts!=8890 and uid_coms=8890 and postacc=1 and postcomacc=1;
+------------+
| count(pid) |
+------------+
|          1 |
|          2 |
+------------+

I want to achieve this

但是,使用联合的常见查询“分组”了这些相同的值。

代码语言:javascript
复制
mysql> select count(pid) from posts
where uid_posts=8890 and postacc=1 and postcomacc=1 ;
+------------+
| count(pid) |
+------------+
|          2 |
+------------+


mysql>  select count(comid) from coms join posts on pid_coms=pid 
where uid_posts!=8890 and uid_coms=8890 and postacc=1 and postcomacc=1;
+--------------+
| count(comid) |
+--------------+
|            2 |
+--------------+



//thay are "grouped" :( below

mysql> select count(pid) from posts
where uid_posts=8890 and postacc=1 and postcomacc=1 
union
select count(comid) from coms join posts on pid_coms=pid 
where uid_posts!=8890 and uid_coms=8890 and postacc=1 and postcomacc=1;
+------------+
| count(pid) |
+------------+
|          2 |
+------------+

如何解开这些结果?

还是我需要重新工作整个查询?

停止播放!工会都必须把我治好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-12 22:55:11

使用union all

代码语言:javascript
复制
select count(pid)
from posts 
where uid_posts = 8890 and postacc = 1 and postcomacc = 1 
union all
select count(comid)
from coms join
     posts
     on pid_coms = pid 
where uid_posts <> 8890 and uid_coms = 8890 and postacc = 1 and postcomacc = 1

我还将添加第二列,以指定哪个计数与哪个查询匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45655593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档