gpt4 book ai didi

mysql - 解码查询结果

转载 作者:行者123 更新时间:2023-11-30 21:54:49 25 4
gpt4 key购买 nike

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

query1 return X 
query2 return X

query1 union query2 renurn only X

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

如果每个请求返回不相等的数据,union 的结果将由两行组成。这是正常的

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

我想实现这个


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

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

如何取消对这些结果的分组?

或者我需要重新处理整个查询吗?

停下! UNION ALL 必须帮助我。

最佳答案

使用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

我还会添加第二列来指定哪个计数与哪个查询相关。

关于mysql - 解码查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45655593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com