建议直接给表结构和例子,这样或许更容易让别人理解一点逻辑并提供其它方案。   建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
   参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382
   
   1. 你的 create table xxx .. 语句
   2. 你的 insert into xxx ... 语句
   3. 结果是什么样,(并给以简单的算法描述)
   4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
   
   这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

解决方案 »

  1.   

    -- ----------------------------
    --  Table structure for `dccuser`
    -- ----------------------------
    DROP TABLE IF EXISTS `dccuser`;
    CREATE TABLE `dccuser` (
      `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `User_name` varchar(32) DEFAULT NULL,
      `User_Pass` varchar(32) DEFAULT NULL,
      `User_Info` varchar(255) DEFAULT NULL,
      `User_Type` set('View','Edit','Review','BasePara','AdvPara','ChildUser','Sensitive') DEFAULT 'View',
      `Parent_id` int(11) unsigned DEFAULT '1',
      `Sensitive_id` int(11) unsigned DEFAULT '0',
      `User_Level` int(11) unsigned DEFAULT '1',
      `Prg_position` mediumtext,
      `User_note` text,
      `Start_time` datetime DEFAULT '2014-01-01 00:00:00',
      `End_time` datetime DEFAULT '2014-01-01 00:00:00',
      PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
    -- ----------------------------
    --  Table structure for `terminal`
    -- ----------------------------
    DROP TABLE IF EXISTS `terminal`;
    CREATE TABLE `terminal` (
      `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `Alias` varchar(128) DEFAULT NULL,
      `BTimeout` int(11) DEFAULT '0',
      `Owner` int(11) DEFAULT NULL,
      `LoginTime` datetime DEFAULT NULL,
      `LogoutTime` datetime DEFAULT NULL,
      `SendBytes` bigint(11) DEFAULT '0',
      `ReceiveBytes` bigint(11) DEFAULT '0',
      `Ver` varchar(16) DEFAULT NULL,
      `IP` varchar(16) DEFAULT NULL,
      `Beattim` int(11) DEFAULT '60',
      `Timeout` int(11) unsigned DEFAULT '180',
      `SN` varchar(64) DEFAULT NULL,
      `Width` int(11) DEFAULT NULL,
      `Height` int(11) DEFAULT NULL,
      `AddrLng` varchar(32) DEFAULT NULL,
      `AddrLat` varchar(32) DEFAULT NULL,
      `Address` varchar(128) DEFAULT NULL,
      `CardStatus` text,
      `CardFunction` longtext,
      `CardInfo` longtext,
      `OtherInfo` longtext,
      PRIMARY KEY (`ID`),
      UNIQUE KEY `SN_sy` (`SN`) USING HASH
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    -- ----------------------------
    --  Table structure for `comlist`
    -- ----------------------------
    DROP TABLE IF EXISTS `comlist`;
    CREATE TABLE `comlist` (
      `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `ComStatus` int(10) DEFAULT '-1',
      `Terminal` int(10) unsigned NOT NULL DEFAULT '0',
      `ComUser` int(10) unsigned DEFAULT NULL,
      `ComNote` varchar(255) DEFAULT NULL,
      `ComType` int(10) unsigned DEFAULT '0',
      `ComContent` text,
      `ComTime` datetime DEFAULT NULL,
      `ComVerifyUser` int(10) unsigned DEFAULT NULL,
      `VerifyTime` datetime DEFAULT NULL,
      `ActiveTime` datetime DEFAULT NULL,
      `FinishTime` datetime DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    -- ----------------------------
    --  Procedure definition for `getChildLst`
    -- ----------------------------
    DROP FUNCTION IF EXISTS `getChildLst`;
    DELIMITER ;;
    CREATE DEFINER=`led`@`%` FUNCTION `getChildLst`(rootID int , levelCnts int) RETURNS varchar(8192) CHARSET ascii
        SQL SECURITY INVOKER
    BEGIN
    #Routine body goes here...
      DECLARE sTemp VARCHAR(8192);
      DECLARE sTempChd VARCHAR(8192);  SET sTemp = '$';
      SET sTempChd =cast(rootId as CHAR);   WHILE sTempChd is not null and levelCnts > 0  DO
         SET sTemp = concat(sTemp,',',sTempChd);
         SELECT group_concat(ID) INTO sTempChd FROM led.dccuser where FIND_IN_SET(Parent_id,sTempChd)>0;
         SET levelCnts = levelCnts - 1 ;
       END WHILE;
       RETURN sTemp;
    END
    ;;
    DELIMITER ;
    我想得到 comlist 所有字段还有dccuser和terminal相应记录部分字段的内容.
      

  2.   

    这和comlist记录多少 没什么大关系  他只取了按照主键排序得前100个
      

  3.   

    find_in_set无法用到索引
    getchildlst:是函数,也有find_in_set
    类似于递归查询
    贴记录出来看看
      

  4.   

    除了comlist所有字段外还要comlist.ComUser字段对应的dccuser表相应ID的的User_info字段, as ComUserInfocomlist.ComVerifyUser字段对应的dccuser表相应ID的的User_info字段 as VerifyUserInfocomlist.terminal字段对应的terminal表相应ID的btimeout字段 as btimeoutcomlist.terminal字段对应的terminal表相应ID的alias字段  as aliascomlist.terminal字段对应的terminal表相应ID的owner字段  as userid
    最后取这张新表的userid在find_in_set(userid,getchildlst(1,255))这个集里面的最后100条记录.
      

  5.   


    你的意思是说 find_in_set 里面用到的"Parent_id"字段加个索引吗?
      

  6.   

     find_in_set是不能用到索引的
      

  7.   

    子查询太多了吧,还不如JOIN。