需要写一个SQL查询
为了得到任何2 EmpCode,每EmpGroup对city = del
EmpCode
EmpGroup
city = del
EmpCodes
表格 :EMP_CITY
EmpCode EmpGroup City 1 A del 2 A del 3 B del 4 B del 5 C del 6 C del 7 A ban 8 A del
输出
EmpGroup: A 1 2 EmpGroup: B 3 4 EmpGroup: C 5 6
试试这个查询
SET @level = 0; SET @group = ''; SELECT EmpCode,EGroup FROM ( SELECT City, EmpCode, EmpGroup, @level := IF(@group = EmpGroup, @level+1, 1) AS level, @group := EmpGroup as EGroup FROM test ORDER BY EmpGroup ) rs WHERE level < 3
EMPCODE EGROUP 1 A 2 A 3 B 4 B 5 C 6 C
这是sqlfiddle演示