root_x Povierennyy root_x Povierennyy - 2 months ago 28
MySQL Question

Ungroup query result

If both requests return the same data, then they are "grouped" into the union.

query1 return X
query2 return X

query1 union query2 renurn only X

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


If each request returns unequal data, the result with the
union
will
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
.




But the common query with the union "grouped" these same values.

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 |
+------------+


How to ungroup these results?



Or do I need to rework the entire query?

STOP! UNION ALL must hepl me.


Answer Source

Use 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

I would also add a second column to specify which count goes with which query.