如何在mysql中动态地应用不同过滤的过滤器

时间:2016-11-19 10:18:16

标签: mysql sql

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')

这是表数据我正在应用此查询仅为每个组提取4个数据,如果记录超过4,则显示为1,我从此查询获得此输出

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  





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

我想使用动态where子句为此应用过滤器我首先尝试根据Id过滤事件但是我无法做到这一点请帮助我在哪里做错了我想要实现的查询

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  and  find_in_set( a.eventID,"1,2")

但我无法获取数据请告诉我哪里出错了。为什么我得到空白数据,而它应该记录该特定eventID记录1,2

1 个答案:

答案 0 :(得分:0)

您还需要将where子句应用于子查询:

SELECT a.*, 
       ( (SELECT COUNT(*)
          FROM eventlist AS c
          WHERE c.GroupID = a.GroupID AND c.eventID IN (1, 2)
         ) > 4
       ) AS ist
FROM eventlist AS a
WHERE (SELECT COUNT(*)
       FROM eventlist AS b 
       WHERE b.GroupID = a.GroupID AND b.eventID >= a.eventID AND
             b.eventID IN (1, 2)
      ) <= 4 AND
      a.eventID IN (1, 2); 

我还将find_in_set()替换为in。这通常是更好的方法,但如果您真的想要,可以使用find_in_set()