如果两个请求都返回相同的数据,那么它们将被"分组"进入工会。
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 |
+------------+
或者我是否需要重新编写整个查询?
答案 0 :(得分:0)
使用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
我还会添加第二列来指定哪个计数与哪个查询相关。