有这俩字段 表名find  怎么求他们集合的差 select b from find where a=1; 
select b from find where a=2;
CREATE TABLE `find` (
  `a` int(10) NOT NULL,
  `b` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;--
-- 导出表中的数据 `find`
--INSERT INTO `find` (`a`, `b`) VALUES
(1, 11),
(1, 12),
(1, 13),
(2, 11),
(2, 12);

解决方案 »

  1.   


    CREATE TABLE qiduf_0927 (
      a int,
      b int
    );--
    -- 导出表中的数据 `qiduf_0927`
    --INSERT INTO qiduf_0927 (a,b) VALUES
    (1, 11);
    INSERT INTO qiduf_0927 (a,b) VALUES
    (1, 12);
    INSERT INTO qiduf_0927 (a,b) VALUES
    (1, 13);
    INSERT INTO qiduf_0927 (a,b) VALUES
    (2, 11);
    INSERT INTO qiduf_0927 (a,b) VALUES
    (2, 12);
    --method one
    select b from qiduf_0927 where a=1
    minus
    select b from qiduf_0927 where a=2
    --result:
    13
    --method two:select a.b from 
    (select a,b from qiduf_0927 where a=1)a,  
    (select a,b from qiduf_0927 where a=2)b
    where  a.b=b.b(+) and b.a is null
    --result
    13
      

  2.   

    select sum(b - a) from find where 条件;
    这样不就OK了,哪那么复杂啊
      

  3.   

    select sum(case find.a when 1 then find.b end)-sum(case find.a when 2 then find.b end) from find
    使用case when then end 语句就OK啦