选择特定列以及其余列,但具有一定的顺序和分组依据

发布于 2024-12-21 05:33:56 字数 697 浏览 2 评论 0原文

我需要你的帮助。

这里我有一些表:

  1. 位置表:ID,代码,位置名称
  2. 房间表:ID,IDLocation,RoomNo
  3. 分配表: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:

  1. Location table : ID, Code, LocationName
  2. Room table : ID, IDLocation, RoomNo
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山有枢 2024-12-28 05:33:56

您必须指定要在查询中分组的列,并且不要使用“*”来选择其他列。

SELECT Assignment.ID, Location.LocationName, Assignment.AssignDate, Room.RoomNo,coulmn1,etch..
FROM Location, Room, Assignment 
WHERE Assignment.IDLocation=Location.ID 
    AND Assignment.HouseNo=Room.ID 
Group By Assignment.IDLocation,Assignment.AssignDate, Room.RoomNo,Column1

问候

You must specify the columns to be group in your query and don't use "*" to select the other columns.

SELECT Assignment.ID, Location.LocationName, Assignment.AssignDate, Room.RoomNo,coulmn1,etch..
FROM Location, Room, Assignment 
WHERE Assignment.IDLocation=Location.ID 
    AND Assignment.HouseNo=Room.ID 
Group By Assignment.IDLocation,Assignment.AssignDate, Room.RoomNo,Column1

Regards

猫九 2024-12-28 05:33:56

根据您的描述,我猜您只有两个选择:

  • 如果 SQL 是即时生成的,您必须迭代用于 SELECT 的字段并将它们放置在您也可以使用的变量中。用于您的GROUP BY
  • 如果没有生成 SQL,那么只要字段列表发生变化,您就必须找到某种方法来修改它。

也就是说,从长远来看,拥有一个始终增加的字段列表听起来并不健康 - 您可能需要检查一下您的设计,看看一切是否都符合预期。

From what you describe, I guess you have only two options:

  • If the SQL is generated on-the-fly, you must iterate through the fields used for the SELECT and place them in a variable that you also use for your GROUP BY.
  • If the SQL is not generated, you will have to find some way to modify it whenever the field list changes.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文