加入后别名为2个表的乘积

时间:2013-11-21 08:04:36

标签: mysql database

好吧,所以我有2个不同的表,我使用内部联接来获得2个表的共同点。加入后我可以为新列添加别名,但我需要计算并显示新列中最常见的值。我无法想办法做到这一点。但是,我通过创建一个新表并计算事件,给出值并删除表来找到解决方法。我真的想知道正确的方法是什么......这是我的代码:

create table result as 
(
   select department.Dcode as de_code, department.Dname as de_name
   from (department join enroll on department.Dcode = enroll.Dcode)
);

SELECT       result.de_name,
             COUNT(result.de_name) AS occurr
    FROM     result
    GROUP BY result.de_name
    ORDER BY occurr DESC
    LIMIT    1;

drop table result;

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

SELECT result.de_name, COUNT(result.de_name) AS occurr
FROM
(select department.Dcode as de_code, department.Dname as de_name from department join enroll on department.Dcode = enroll.Dcode) as result
GROUP BY result.de_name
ORDER BY occurr DESC

此处resultINNER JOIN返回的记录的别名。