对两列进行查询,就好像它们是一列

问题描述

| 有2列包含相同类型的数据:
col_a  col_b ...
Bob    Tim   ..
John   Frank .
Bob    John
John   Tim
Bob    Bob
Frank  John
现在我要执行的查询是这样的(计数出现次数:(Bob,3),(John,2),..):
SELECT col_a,count(*) AS total FROM table1 GROUP BY col_a
但是,我不想在col_a上同时运行它,而是想同时在col_a和col_b上运行它((Bob,4),(John,4),..) 任何帮助表示赞赏 编辑:     谢谢大家。 再次感谢     

解决方法

Select Z.name,Count(*) As Total
From    (
        Select col_a As name
        From total
        Union All
        Select col_b
        From total
        ) As Z
Group By Z.name
    ,
select Name,count(*) as Total
from (
    select col_a as Name from MyTable
    union all
    select col_b from MyTable
) a
group by Name
    ,根据您的“ 4”列,我认为您需要对子查询进行分组:
select idx,sum(count)
from (
    select col_a as idx,count(*) as count
    from table
    union all
    select col_b as idx,count(*) as count
    where col_a <> col_b -- avoid dups
    ) as counts
group by idx
要么:
select idx,count(*)
from (
    select col_a as idx
    from table
    union all
    select col_b as idx
    where col_a <> col_b -- avoid dups
    ) as counts
group by idx
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...