我有两个名为user和submission的表。我想自然地加入他们。然后按列分组。然后选择两列。给他们别名。然后按给定的别名对它们进行排序。
我的代码如下。
select name, count(result) as name, tried from user
natural join submission group by name sort by tried;
但是,这不起作用。我收到一个错误:
1064 - 您的SQL语法出错;查看与MySQL服务器版本对应的手册,以便在第2行“按计数排序(结果)LIMIT 0,30”附近使用正确的语法
答案 0 :(得分:2)
您有两个同名的结果:
select name, <-- 1st "anme"
count(result) as name, <-- 2nd name
tried from user natural join submission group by name order by tried;
修复很简单,因为你需要做的就是更改COUNT
结果的别名select name,
count(result) as result_count,
tried from user natural join submission group by name order by tried;
答案 1 :(得分:1)
select name,
count(result) as result_count,
tried from `user` natural join submission group by name ORDER by tried;