选择特定列以及其余列,但具有一定的顺序和分组依据
我需要你的帮助。
这里我有一些表:
- 位置表:ID,代码,位置名称
- 房间表:ID,IDLocation,RoomNo
- 分配表:ID,IDLocation,RoomNo,AssignDate,AssignMonth,AssignYear,ItemA,ItemB,ItemC,...(项目列将始终增加/无限数量的项目列)
我想要一个具有以下顺序序列的选择语句:
Assignment.ID,Location.LocationName, Assignment.AssignDate、Room.RoomNo 以及按Assignment.IDLocation 分组的其余项目列
我之前提出过这个查询:
SELECT Assignment.ID, Location.LocationName, Assignment.AssignDate, Room.RoomNo, *
FROM Location, Room, Assignment
WHERE Assignment.IDLocation=Location.ID
AND Assignment.HouseNo=Room.ID
Group By Assignment.IDLocation
但是我收到了这个警告:“无法对使用 '*' 选择的字段进行分组”
I need your help.
Here I have some tables:
- Location table : ID, Code, LocationName
- Room table : ID, IDLocation, RoomNo
- Assignment table : ID, IDLocation, RoomNo, AssignDate, AssignMonth, AssignYear, ItemA, ItemB, ItemC, .... (the item columns will always increase/indefinite number of item columns)
I'd like to have a select statement with this sequence of order:
Assignment.ID, Location.LocationName, Assignment.AssignDate, Room.RoomNo, and the rest of the item columns grouped by Assignment.IDLocation
I came up with this query before:
SELECT Assignment.ID, Location.LocationName, Assignment.AssignDate, Room.RoomNo, *
FROM Location, Room, Assignment
WHERE Assignment.IDLocation=Location.ID
AND Assignment.HouseNo=Room.ID
Group By Assignment.IDLocation
However I got this warning instead : "Cannot group on fields selected with '*'"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须指定要在查询中分组的列,并且不要使用“*”来选择其他列。
问候
You must specify the columns to be group in your query and don't use "*" to select the other columns.
Regards
根据您的描述,我猜您只有两个选择:
GROUP BY
。也就是说,从长远来看,拥有一个始终增加的字段列表听起来并不健康 - 您可能需要检查一下您的设计,看看一切是否都符合预期。
From what you describe, I guess you have only two options:
SELECT
and place them in a variable that you also use for yourGROUP BY
.That said, having a field list that always increases doesn't sound healthy in the long run - you might want to take a look at your design and see if everything is as it should be.