Mysql 使用UNION ALL时,如何使数据返回0?

Mysql 使用UNION ALL时,如何使数据返回0?,mysql,join,count,frequency,Mysql,Join,Count,Frequency,使用UNION ALL时,数据不会返回0。例如,如果“表内高分”的频率为零,它将不返回任何内容。这就是我想在代码中应用外部联接的原因 表1 id | myid | score | --------------------------------------- 1 | 20 | high | 2 | 20 | low | 3 | 21

使用UNION ALL时,数据不会返回0。例如,如果“表内高分”的频率为零,它将不返回任何内容。这就是我想在代码中应用外部联接的原因

表1

  id    |   myid     |    score       |
---------------------------------------
1       |    20      |    high        |
2       |    20      |    low         |
3       |    21      |    average        |
4       |    21      |    high        |
5       |    21      |    low         |
表2

 id2    |   myid     |   score      |
-------------------------------------
1       |    21      |   high       |
2       |    21      |   low        |
3       |    20      |   low        |
4       |    20      |   low        |
5       |    20      |   low        |
我为myid=20获得的输出

myid     |  score       | f  |
-------------------------------
 20      |   low        | 4   |
 20      |   high       | 1   |  
所需输出

 myid    |  score       | f  |
-------------------------------
 20      |   low        | 4   |
 20      |   high       | 1   |  
 20      |   average    | 0   | 
我的代码:

select myid, score, count(*) as f
from
(
  select myid, score from table1 where myid=12
  union all
  select myid, score from table2 where myid=12
) unioned
group by myid, score

您应该取两个表的并集,然后在此基础上进行聚合:

SELECT
    myid,
    score,
    COUNT(*) AS f
FROM
(
    SELECT myid, score FROM table1
    UNION ALL
    SELECT myid, score FROM table2
) t
GROUP BY
    myid,
    score;

您应该取两个表的并集,然后在此基础上进行聚合:

SELECT
    myid,
    score,
    COUNT(*) AS f
FROM
(
    SELECT myid, score FROM table1
    UNION ALL
    SELECT myid, score FROM table2
) t
GROUP BY
    myid,
    score;

您可以使用
UNION
两个表,然后使用
groupby
myid。例如:

select
  myid, score, count(*) as f
from (
  select myid, score from table1
  union all
  select myid, score from table2
) x
group by myid, score
编辑

如果要在没有行的情况下始终将
值显示为零,则可以使用
左连接来执行此操作:

select myid, score, sum(c) as f
from (
  select 'low' as score union select 'high'
) s
left join (
  select myid, score, 1 as c from table1
  union all
  select myid, score, 1 from table2
) x on x.score = s.score
group by myid, score

请注意,我没有测试最后一个查询。

您可以使用
UNION
两个表,然后使用
groupby
myid。例如:

select
  myid, score, count(*) as f
from (
  select myid, score from table1
  union all
  select myid, score from table2
) x
group by myid, score
编辑

如果要在没有行的情况下始终将
值显示为零,则可以使用
左连接来执行此操作:

select myid, score, sum(c) as f
from (
  select 'low' as score union select 'high'
) s
left join (
  select myid, score, 1 as c from table1
  union all
  select myid, score, 1 from table2
) x on x.score = s.score
group by myid, score

请注意,我没有测试最后一个查询。

UNION ALL
查询是不够的,因为它可能不包含
分数的所有可能值:
“高”
“低”
“平均值”

使用一个返回所有这些可能值的查询,并左键联接
UNION all
查询:

select s.score, count(t.score) f
from (select 'high' score union all select 'low' union all select 'average') s
left join (
  select * from Table1 where myid = 20
  union all
  select * from Table2 where myid = 20
) t on t.score = s.score
group by s.score
请参阅。
结果:


UNION ALL
查询是不够的,因为它可能不包含
score
的所有可能值:
'high'
'low'
'average'

使用一个返回所有这些可能值的查询,并左键联接
UNION all
查询:

select s.score, count(t.score) f
from (select 'high' score union all select 'low' union all select 'average') s
left join (
  select * from Table1 where myid = 20
  union all
  select * from Table2 where myid = 20
) t on t.score = s.score
group by s.score
请参阅。
结果:


如果我使用union,它将不会为所选对象返回0score@huhu我的答案应该与您提供的样本数据相符。您能否提供其他数据来说明我的答案存在问题?您的方法运行良好,但如果数据(如
low
为零或未占用)为零,则不会返回0如果我使用union,则不会为所选的返回0score@huhu我的答案应该与您提供的样本数据相符。您能否提供其他数据来说明我的答案存在问题?您的方法运行良好,但如果数据(如
low
为零或未占用
JOIN
=水平连接),则不会返回0,
UNION
=垂直添加数据-
分组依据
和聚合通常垂直工作。你的意思是我应该使用UNION?
JOIN
=水平连接,
UNION
=垂直追加数据-
分组依据
,聚合通常垂直工作。你的意思是我应该使用UNION?我以前尝试过这些方法,但是,例如:如果
的频率为零。它不会返回值吗?你有什么建议吗?我添加了一个额外的问题,这可能是你需要的。不过,您的示例数据并没有显示您所询问的案例。为了避免混淆,我建议您将案例添加到问题中……我已经编辑了我的问题……对不起……我也混淆了我的问题我以前尝试过这些方法,但是,例如:如果
的频率为零。它不会返回值吗?你有什么建议吗?我添加了一个额外的问题,这可能是你需要的。不过,您的示例数据并没有显示您所询问的案例。为了避免混淆,我建议您将案例添加到问题中……我已经编辑了我的问题……对不起……我也混淆了我的问题