2016-11-19 46 views
1
CREATE TABLE `EventList` (
    `GroupID` int(11) NOT NULL , 

    `eventID` int(11) NOT NULL , 
    `EMPNAME` varchar(20) NOT NULL, 
    `EMPAGE` int(11) NOT NULL, 
    `SALARY` bigint(20) NOT NULL, 
    `ADDRESS` varchar(20) NOT NULL, 
    `empaddress` varchar(255) DEFAULT NULL, 
    `file_data` tinyblob 

) 


insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (1,2,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (1,5,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (1,7,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (1,8,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (1,9,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (2,15,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (2,16,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (3,19,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (3,22,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (3,24,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (3,27,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (3,29,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,31,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,32,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,33,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,34,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,35,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,36,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,37,"anil",5,556,'del','del','//document') 
insert into eventlist(GroupID,eventID,EMPNAME,EMPAGE,SALARY,ADDRESS,empaddress,file_data) value (0,39,"anil",5,556,'del','del','//document') 

這是我的表結構查詢:如何獲取數據的頂部在MySQL 4的記錄

GroupID eventID EMPNAME EMPAGE SALARY ADDRESS empaddress file_data 
1   2 anil 5  556  del  del   ... 
1   5 anil 5  556  del  del   ... 
1   7 anil 5  556  del  del   ... 
1   8 anil 5  556 del  del   ... 
1   2 anil 5  556  del  del   ... 
2   15 anil 5  556  del  del   ... 
2   16 anil 5  556  del  del   ... 
3   19 anil 5  556 del  del   ... 
3   22 anil 5  556  del  del   ... 
3   24 anil 5  556  del  del   ... 
3   27 anil 5  556  del  del   ... 
3   29 anil 5  556 del  del   ... 
0   31 anil 5  556  del  del   ... 
0   32 anil 5  556  del  del   ... 
0   33 anil 5  556  del  del   ... 
0   34 anil 5  556 del  del   ... 
0   35 anil 5  556 del  del   ... 
0   36 anil 5  556 del  del   ... 
0   37 anil 5  556 del  del   ... 

目前的數據來的時候有寫這樣的查詢來獲取這樣每個組,我們必須取如果小於4事件則只我們獲取只有2記錄

輸出的願望,我想只有4事件

GroupID eventID EMPNAME EMPAGE SALARY ADDRESS empaddress ist 
    1   2 anil 5 556  del  del  ...  1 
    1   5 anil 5 556  del  del  ...  1 
    1   7 anil 5 556  del  del  ...  1 
    1   2 anil 5 556  del  del  ...  1 
    2   15 anil 5 556  del  del  ...  0 
    2   16 anil 5 556  del  del  ...  0 
    3   19 anil 5 556  del  del  ...  1 
    3   22 anil 5 556  del  del  ...  1 
    3   24 anil 5 556  del  del  ...  1 
    3   29 anil 5 556  del  del  ...  1 
    0   31 anil 5 556  del  del  ...  1 
    0   32 anil 5 556  del  del  ...  1 
    0   33 anil 5 556  del  del  ...  1 
    0   34 anil 5 556  del  del  ...  1 

我想要這樣的記錄哪裏是如果特定組有超過4記錄那麼它應該是1 請建議我如何寫這個查詢,以便我可以得到這樣的輸出。正如我在評論中提到

+0

@EhsanT我是confuesd如何從員工那裏EVENTID極限凌晨4時試圖寫得到這樣的輸出 –

+0

選擇羣ID,事件ID查詢,EMPNAME \t,EMPAGE \t,薪金\t但是這不會工作 –

+0

@EhsanT此查詢的任何解決方案? –

回答

2

OK,它的第一個更好的閱讀中this question

提供的解決方案,但它會適合您的需求將是這樣的查詢:

SELECT a.*, 
    IF((SELECT COUNT(*) FROM eventlist AS c 
     WHERE c.GroupID = a.GroupID) > 4, 1, 0) AS ist 
FROM eventlist AS a 
WHERE 
    (SELECT COUNT(*) FROM eventlist AS b 
    WHERE b.GroupID = a.GroupID and b.eventID >= a.eventID) <= 4 
ORDER BY a.GroupID, a.eventID 
+0

偉大thanx其工作,但我將如何顯示的東西,請建議我 –

+0

哦,我沒有看到這部分的問題。讓我更改答案並在幾分鐘內將其添加到查詢中 – EhsanT

+0

4,1,0爲什麼你給出了這個 –

相關問題