根据计数加入3个表

时间:2016-06-22 10:12:07

标签: sql

有3个表

ITEM -itemID

2.INVENTORY

-quantity

3.CHARACTERS

-charID

项目1 ---许多存货8 ---- 1字符

我想找到堆叠在角色库存中的前十大堆叠物品以及谁将此物品堆放在其库存中。所以最后它会像itemID 1有4个堆栈,这些charID有项目,itemID 2有3个堆栈,这些charID有项目。在表格中。

这是我目前在尝试这样做一段时间后所处的位置

Column 'CHARACTERS.charName' is invalid in the select list because it is not         
contained in either an aggregate function or the GROUP BY clause.

我收到了一个汇总错误,我不知道如何修复此查询

<div id="gridContent" style="font-family: Arial; padding: 20px; overflow:auto;height:380px" class="col-md-12">
@grid.GetHtml(tableStyle: "webgrid-table",
    headerStyle: "webgrid-header",
    footerStyle: "webgrid-footer",
    alternatingRowStyle: "webgrid-alternating-row",
    selectedRowStyle: "webgrid-selected-row",
    rowStyle: "webgrid-row-style",
    mode: WebGridPagerModes.All,
    columns:
        grid.Columns(
         grid.Column("NodeNumber", "Node Nr.", format: @<text>  <span class="display-mode">@item.NodeNumber </span> <label id="NodeNumber" class="edit-mode">@item.NodeNumber</label> </text>, style: "col1Width"),
         grid.Column("Accumulated_Length", "Accumulated Length", format: @<text>  <span class="display-mode"> <label id="lblAccumulated_Length">@item.Accumulated_Length</label> </span> <input type="text" id="Accumulated_Length" value="@item.Accumulated_Length" class="edit-mode" /></text>, style: "col2Width"),
         grid.Column("Elevation", "Elevation", format: @<text> <span class="display-mode"> <label id="lblElevation">@item.Elevation</label> </span>  <input type="text" id="Elevation" value="@item.Elevation" class="edit-mode" /> </text>, style: "col2Width"),
         grid.Column("Pipe_Outside_Diameter", "Pipe Outside Diameter", format: @<text> <span class="display-mode"> <label id="lblPipe_Outside_Diameter">@item.Pipe_Outside_Diameter</label> </span>  <input type="text" id="Pipe_Outside_Diameter" value="@item.Pipe_Outside_Diameter" class="edit-mode" /> </text>, style: "col2Width"),
         grid.Column("Wall_Thickness", "Wall Thickness", format: @<text> <span class="display-mode"> <label id="lblWall_Thickness">@item.Wall_Thickness</label> </span>  <input type="text" id="Wall_Thickness" value="@item.Wall_Thickness" class="edit-mode" /> </text>, style: "col2Width"),
         grid.Column("Control_Point_Description", "Control Point Description", format: @<text> <span class="display-mode"> <label id="lblControl_Point_Description">@item.Control_Point_Description</label> </span>  <input type="text" id="Control_Point_Description" value="@item.Control_Point_Description" class="edit-mode" /> </text>, style: "col2Width"),
         grid.Column("Control_Point_Size", "Control Point Size", format: @<text> <span class="display-mode"> <label id="lblControl_Point_Size">@item.Control_Point_Size</label> </span>  <input type="text" id="Control_Point_Size" value="@item.Control_Point_Size" class="edit-mode" /> </text>, style: "col2Width"),
         grid.Column("", format: @<text>
                <input type="hidden" class="fkiProjectID" value="@item.fkiProjectID" />
                <input type="hidden" class="pkiPipeline" value="@item.pkiPipeline" />
                <div class="container" style="width:120px">
                    <div class="btn-group-xs">
                        <button data-loading-text="Loading..." class="edit-user display-mode btn btn-default2" autocomplete="off">Edit</button>
                        <button class="delete-user display-mode btn btn-default2">Delete</button>
                    </div>
                </div>
        </text>, style: "col3Width", canSort: false)
       ))
</div>

@Html.ActionLink("Pipeline", "ExportPDF", new { ProjectID = Model.fkiProjectID }, new { target = "_blank" })

欢迎任何帮助谢谢:)。如果我的问题格式不符合标准,请留下我可以改进的评论。

2 个答案:

答案 0 :(得分:2)

只需将charName添加到GROUP BY子句或使用聚合函数包装它:

SELECT COUNT(INVENTORY.itemID) as Stacks, ITEM.itemID, MAX(CHARACTERS.charName)
FROM INVENTORY
INNER JOIN ITEM
 ON INVENTORY.itemID = ITEM.itemID
JOIN CHARACTERS
 ON INVENTORY.charID = CHARACTERS.charID
WHERE INVENTORY.quantity>1
GROUP BY ITEM.itemID
ORDER BY  Stacks DESC
GO

据我所知,只有MySQL接受GROUP BY不包含整个选择列集。每列必须位于GROUP BY子句中,或者包含聚合函数,如COUNT/MAX/MIN..

答案 1 :(得分:0)

尝试在group by子句中添加'CHARACTERS.charName'。

您的最终查询应该是:

SELECT COUNT(INVENTORY.itemID) as Stacks, ITEM.itemID, CHARACTERS.charName
FROM INVENTORY
INNER JOIN ITEM
ON INVENTORY.itemID = ITEM.itemID
JOIN CHARACTERS
ON INVENTORY.charID = CHARACTERS.charID
WHERE INVENTORY.quantity>1
GROUP BY ITEM.itemID, CHARACTERS.charName
ORDER BY  Stacks DESC
GO