select worksalary,avg(worksalary),workname,worksex,innum from worker,inhotel where worker.worknum=inhotel.worknum group by avg(worksalary) order by worksalary asc; ERROR 1056 (42000): Can't group on 'avg(worksalary)'
时间: 2024-02-28 07:55:51 浏览: 87
这个错误是因为你尝试在 GROUP BY 子句中使用了聚合函数 avg(worksalary),而正确的做法是在 SELECT 子句中使用聚合函数并在 GROUP BY 子句中添加非聚合字段。另外,如果你想按照 worksalary 升序排列,应该在 ORDER BY 子句中写成 worksalary ASC,而不是 avg(worksalary) ASC。
你可以尝试如下 SQL 语句:
```
SELECT worker.worksalary, AVG(worker.worksalary), worker.workname, worker.worksex, inhotel.innum
FROM worker
INNER JOIN inhotel
ON worker.worknum = inhotel.worknum
GROUP BY worker.worksalary, worker.workname, worker.worksex, inhotel.innum
ORDER BY worker.worksalary ASC;
```
这个语句会先将 worker 表和 inhotel 表进行内连接,然后按照 worksalary、workname、worksex 和 innum 字段进行分组,并计算每组的平均 worksalary。最后按照 worksalary 升序排列。
相关问题
select cusnum,workname from customer,worker where cusnum=(select cusnum from inhotel where customer.cusname=inhotel.cusname) -> workname=(select workname from worker.worknum=inhotel.worknum); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'workname=(select workname from worker.worknum=inhotel.worknum)' at line 2 mysql>
在这个 SQL 语句中,您需要使用 JOIN 子句来联接多个表,而不是使用嵌套的子查询。您可以尝试以下 SQL 语句:
```
SELECT customer.cusnum, worker.workname
FROM customer
JOIN inhotel ON customer.cusnum = inhotel.cusnum
JOIN worker ON worker.worknum = inhotel.worknum;
```
这个语句将会从 `customer`、`inhotel` 和 `worker` 表中检索数据,并使用 `JOIN` 子句将它们连接起来。
select room.roomtype,inhotel.innum,worker.workname from room -> join inhotel on inhotel.roomnum=room.roomnum join (select workname from worker)workname on inhotel.worknum=worker.worknum; ERROR 1054 (42S22): Unknown column 'worker.workname' in 'field list'
根据你提供的 SQL 语句,错误提示是无法识别 "worker.workname" 这个列名,这是因为你在子查询中创建了一个名为 "workname" 的子查询表,但你没有给它一个别名,所以在主查询中无法引用它。
可以尝试修改 SQL 语句,给子查询表 "workname" 添加一个别名,例如:
```
SELECT room.roomtype, inhotel.innum, w.workname
FROM room
JOIN inhotel ON inhotel.roomnum = room.roomnum
JOIN (SELECT workname FROM worker) AS w ON inhotel.worknum = w.worknum;
```
这样应该可以解决问题。
阅读全文