這是材料表查找最大
ID DCNumber InvoiceNumber DeviceModel DeviceSerialNumber
1 NULL 1001 Audio Door Phone 1
2 NULL 1001 Audio Door Phone 2
3 NULL 1001 Audio Door Phone 3
4 NULL 1001 Audio Door Phone 4
5 NULL 1001 Audio Door Phone 5
6 NULL 1001 Audio Door Phone 6
7 NULL 1001 Audio Door Phone 7
8 NULL 1001 Audio Door Phone 8
9 NULL 1001 Forgo cleaning kit.HDP-5000.(89200) 1
10 NULL 1001 Forgo cleaning kit,HDP-5000.(89200) 2
11 NULL 1001 Forgo cleaning kit.HDP-5000.(89200) 3
12 NULL 1001 Forgo cleaning kit HDP-5000.(89200) 4
13 NULL 1001 Forgo cleaning ldt.HDP-5000.(89200) 5
14 NULL 1001 Forgo cleaning kit.HDP-5000.(89200) 6
15 NULL 1001 Forgo cleaning kit.HDP-5000.(89200) 7
16 NULL 1001 Forgo cleaning kit,HDP-5000.(89200) 8
17 NULL 1001 Forgo cleaning kit.HDP-5000.(89200) 9
18 NULL 1001 Forgo cleaning kit,HDP-5000.(89200) 10
19 NULL 1001 Forgo cleaning kit.HDP-5000.(89200) 11
20 NULL 1001 Forgo cleaning kit HDP-5000.(89200) 12
21 NULL 1001 IR Bullet Camera TC62L2A 1
與ID爲PrimaryKey的,我試圖執行下面的查詢..
select DeviceModel,group_concat(`DeviceSerialNumber` separator ',') ``as`SerialNumbers`,max(DeviceSerialNumber) as 'Quantity' from materials group by InvoiceNumber,DeviceModel having InvoiceNumber= '1001';
和下面是輸出
但質量列返回錯誤的數據..我需要最大的deviceserialnumber作爲特定設備的質量。即在這種情況下8,12,20,40
請注意,OP使用的是VARCHAR而不是INTEGERS。 – Strawberry 2015-02-05 14:40:21
@jpw考慮使用「where」而不是「having」。 「有」是如此怪異慢,相比「其中」 – Dmitri 2015-02-05 14:41:44
@Strawberry請忽略Devicemodelname您可以分配簡單名稱... – Athul 2015-02-05 14:50:25