0

我有两个名为用户和提交的表。我想自然地加入他们。然后按列对它们进行分组。然后选择两列。给他们别名。然后按给定的别名对它们进行排序。

我的代码已在下面给出。

select name, count(result) as name, tried from user
natural join submission group by name sort by tried; 

但是,这是行不通的。我收到一个错误:

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的 'sort by count(result) LIMIT 0, 30' 附近使用正确的语法

4

2 回答 2

3

您有两个具有相同名称的结果:

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; 
于 2012-11-18T02:51:19.730 回答
1
select name, 
count(result) as result_count, 
tried from `user` natural join submission group by name ORDER by tried;
于 2012-11-18T03:08:46.337 回答