name    id
成都    1
成都    2
成都    3
商场    2
商场    3
商场    4
人民    3
人民    4
人民    5
用sql查找出  '成都' 和 '商场' 和 '人民' 的交集  ,能运行的哈!!肯能还会添加条件 ('成都'‘新世纪’‘人民’‘商场’)id
3

解决方案 »

  1.   

    select id from tt where name in('成都','商场','人民')
    group by id having count(*)>=2
      

  2.   

    修改:
    select id from tt where name in('成都','商场','人民')
    group by id having count(*)>=3
      

  3.   

    存储过程:
    DELIMITER $$
    drop procedure if exists ttcfcx$$
    CREATE  PROCEDURE `zz`.`ttcfcx`(in ee varchar(200))
         BEGIN
    set @ff1=length(ee)-length(replace(ee,',',''))+1;
    set @ff=concat('select id from ttcf where name in (',ee,') group by id having count(*)>=',@ff1);
    select @ff;
    prepare rr from @ff2;
    execute rr;
        END$$DELIMITER ;call ttcfcx("'成都','商场','人民'")
    假设以逗号为分隔符
      

  4.   

    对不起,不对啊!。。
    我意思是,取他们结果的  交集 (id列)  name    id
    成都    1
    成都    2
    成都    3——(number1)
    商场    2
    商场    3——(number2)
    商场    4
    人民    3——(number3)
    人民    4
    人民    5 交集!
      

  5.   

    wwwwb我一会来结贴哈!!
    谢谢了!
      

  6.   


    前辈,我怎么创建不了  这个存储过程啊?  我的数据库 是  lucenesearch    表是 testa !!! 
      

  7.   

    我是在SQLYOG中创建,你是在MYSQL命令行中?
      

  8.   

    晕了!  创建的时候弹错误!,但是 用 SHOW CREATE PROCEDURE lucenesearch.ttcfcx;看又创建起了。!!
      

  9.   

    呵呵,
    prepare rr from @ff2->prepare rr from @ff
      

  10.   

    OR
    DELIMITER $$DROP PROCEDURE IF EXISTS `zz`.`ttZfcx`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `ttZfcx`(in ee varchar(200))
    BEGIN
    DECLARE DD INT;
    DECLARE DD1 VARCHAR(200);
    Set DD=length(ee)-length(replace(ee,',',''))+1;
    SET DD1=CONCAT('select id from ttGH4 where name in (',ee,') group by id having count(*)>=',DD);
    SET @FF=DD1;
    prepare rr from @ff;
    execute rr; 
        END$$DELIMITER ;
      

  11.   

    hehe,楼主给 wwwwb
    结贴吧。
    不过,wwwwa 和 wwwwb 这两个帐号都这面高的级别,真是不容易啊。