問(wèn)題描述
Model_Tabl ( ID, ModelName, ModelQuantity)
Items_Tabl ( I_Code, IName, ID)
將新行插入 (Model_Table) 后 - 觸發(fā)器將多行插入 (Items_Table) 取決于 (Model_Table) 中的 ModelQuantity,直到現(xiàn)在它的工作正常
after inserting new row into (Model_Table) - Triggers insert multi row into (Items_Table) Depend on ModelQuantity from (Model_Table) , and until now its work fine
I Created "選擇不同的 ModelName , Sum(ModelQuantity) group by ModelName"
我的結(jié)果很好
I Created "select distinct ModelName , Sum(ModelQuantity) group by ModelName"
and i got result fine
我的問(wèn)題是:
當(dāng)我從 (DISTINCT) 查詢中選擇模型名稱時(shí),我想知道我從 (Model_Table) 中選擇了哪個(gè) (ID)
When i select model name from (DISTINCT) query i want to know which (ID) I selected from (Model_Table)
Model_ID (TO) Model_Name = 1 (TO) 許多ty
Model_ID (TO) Model_Name = 1 (TO) Many ty
推薦答案
首先,你不需要 DISTINCT
那里,所以你可以擺脫它.然后,你可以試試這個(gè):
First of all, you don't need the DISTINCT
there, so you can get rid of it. And then, you can try this:
SELECT ID, ModelName, SUM(ModelQuantity) Quantity
FROM Model_Tabl
GROUP BY ID, ModelName
好的,正如我在評(píng)論中解釋的那樣,如果您有多個(gè)同名的 ID,則需要選擇一個(gè),即最大 ID 或最小 ID(最終還是沒(méi)有意義)僅在您實(shí)際需要識(shí)別該 ID 時(shí)選擇一個(gè) ID),但是,這里是:
Ok, as I explained on my comment, if you have multiple IDs with the same name, you need to choose one, either the max or the min ID (it still doesn't make sense that you are going to end up just choosing one ID when you actually need to identify that ID), but well, here it is:
SELECT MIN(ID) ID, ModelName, SUM(ModelQuantity) Quantity
FROM Model_Tabl
GROUP BY ModelName
這篇關(guān)于從不同的結(jié)果中選擇后獲取身份的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!