如果两个请求都返回相同的数据,那么它们将被“分组”到联合中。
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。
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。
但是,使用联合的常见查询“分组”了这些相同的值。
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 |
+------------+如何解开这些结果?
还是我需要重新工作整个查询?
停止播放!工会都必须把我治好。
发布于 2017-08-12 22:55:11
使用union all
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我还将添加第二列,以指定哪个计数与哪个查询匹配。
https://stackoverflow.com/questions/45655593
复制相似问题