字段是这样:
产品id, 产品价格, 价格发布时间.
同一款产品不存在, 价格发布时间相邻, 但是价格相同的记录.
价格>=0;如果某款产品, 只有一个记录, 则认为它没有降价.第一个是: 取出最新降价的10款产品. (SELECT时, 要取出两次的价格)
第二个是: 取出最新降价, 但是价格没有降到0的10款产品.

解决方案 »

  1.   

    1:取出最新降价的10款产品select id ,count(*) from tbname group by id having count(*)>1 order by ttime desc limit 10;
      

  2.   

    product price time
    1 0 2010-01-01
    1 1 2010-01-02
    1 2 2010-01-03
    1 3 2010-01-04
    1 2 2010-01-05
    1 1 2010-01-06
    1 0 2010-01-07
    2 2 2010-01-01
    2 1 2010-01-02
    3 0 2010-01-01
    4 1 2010-01-01
    5 1 2010-01-01
    5 2 2010-01-02
      

  3.   

    select a.product,a.time,b.time ,a.price,b.price
    from (
    select a.product,a.time,b.time ,a.price,b.price
    from 一个价格表 a ,一个价格表 b
    where a.product=b.product and a.time>b.time
    order by a.product,a.time desc,b.time desc
    ) t
    where a.price<b.price
    group by a.product
      

  4.   


    select a.product,a.time,b.time ,a.price,b.price
    from (
    select a.product,a.time,b.time ,a.price,b.price
    from 一个价格表 a ,一个价格表 b
    where a.product=b.product and a.time>b.time
    order by a.product,a.time desc,b.time desc
    ) t
    where a.price<b.price
    and a.price>0
    group by a.product
      

  5.   

    由于楼主没有必要数据无法测试验证 (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  6.   

    贴建表及插入记录的SQL,及要求结果出来看看
      

  7.   

    我做了数据,但是还没求出结果。
    狼头哥的写法有问题。FORM 后带的是表Tcreate table pro(product int,price int,`time` date);
    delete from pro
    insert into pro values
    (1 ,0, '2010-01-01'),
    (1 ,1 ,'2010-01-02'),
    (1 ,2 ,'2010-01-03'),
    (1 ,3 ,'2010-01-04'),
    (1 ,2 ,'2010-01-05'),
    (1 ,1 ,'2010-01-06'),
    (1 ,0 ,'2010-01-07'),
    (2 ,2 ,'2010-01-01'),
    (2 ,4 ,'2010-01-02'),
    (3 ,0 ,'2010-01-01'),
    (4 ,1 ,'2010-01-01'),
    (5 ,1 ,'2010-01-01'),
    (5, 2 ,'2010-01-02')
      

  8.   

    我做了数据,但是还没求出结果。
    狼头哥的写法有问题。FORM 后带的是表Tcreate table pro(product int,price int,`time` date);
    delete from pro
    insert into pro values
    (1 ,0, '2010-01-01'),
    (1 ,1 ,'2010-01-02'),
    (1 ,2 ,'2010-01-03'),
    (1 ,3 ,'2010-01-04'),
    (1 ,2 ,'2010-01-05'),
    (1 ,1 ,'2010-01-06'),
    (1 ,0 ,'2010-01-07'),
    (2 ,2 ,'2010-01-01'),
    (2 ,4 ,'2010-01-02'),
    (3 ,0 ,'2010-01-01'),
    (4 ,1 ,'2010-01-01'),
    (5 ,1 ,'2010-01-01'),
    (5, 2 ,'2010-01-02')
      

  9.   

    mysql> select * from pro;
    +---------+-------+------------+
    | product | price | time       |
    +---------+-------+------------+
    |       1 |     0 | 2010-01-01 |
    |       1 |     1 | 2010-01-02 |
    |       1 |     2 | 2010-01-03 |
    |       1 |     3 | 2010-01-04 |
    |       1 |     2 | 2010-01-05 |
    |       1 |     1 | 2010-01-06 |
    |       1 |     0 | 2010-01-07 |
    |       2 |     2 | 2010-01-01 |
    |       2 |     4 | 2010-01-02 |
    |       3 |     0 | 2010-01-01 |
    |       4 |     1 | 2010-01-01 |
    |       5 |     1 | 2010-01-01 |
    |       5 |     2 | 2010-01-02 |
    +---------+-------+------------+
    13 rows in set (0.00 sec)mysql>
    mysql> select product,atime,btime ,aprice,bprice
        -> from (
        ->     select a.product,a.time as atime,b.time as btime ,a.price as aprice,b.price as bprice
        ->     from pro a ,pro b
        ->     where a.product=b.product and a.time>b.time
        ->     order by a.product,a.time desc,b.time desc
        -> ) t
        -> where aprice<bprice
        -> group by product
        -> order by atime desc limit 10;
    +---------+------------+------------+--------+--------+
    | product | atime      | btime      | aprice | bprice |
    +---------+------------+------------+--------+--------+
    |       1 | 2010-01-07 | 2010-01-06 |      0 |      1 |
    +---------+------------+------------+--------+--------+
    1 row in set (0.00 sec)mysql>
      

  10.   


    这也是为什么我总是说  (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  11.   

    已经结贴了啊
    周末弄了一下,还是贴出来,也许能够给你点提示;
    1:测试数据create table pro(product int,price int,`time` date);
    delete from pro;
    insert into pro values
    (1 ,0, '2010-01-01'),
    (1 ,1 ,'2010-01-02'),
    (1 ,2 ,'2010-01-03'),
    (1 ,30 ,'2010-01-04'),
    (1 ,20 ,'2010-01-05'),
    (1 ,10 ,'2010-01-06'),
    (1 ,8 ,'2010-01-07'),
    (2 ,6 ,'2010-01-01'),
    (2 ,4 ,'2010-01-02'),
    (2 ,5 ,'2010-01-03'),
    (2 ,3 ,'2010-01-05'),
    (3 ,0 ,'2010-01-01'),
    (4 ,3 ,'2010-01-01'),
    (5 ,50 ,'2010-01-01'),
    (5, 20 ,'2010-01-02'),
    (5, 10 ,'2010-01-03'),
    (5, 9 , '2010-01-04')
    2:存储过程drop procedure if exists get_price_down;
    create procedure get_price_down()
    begin
       DECLARE done INT DEFAULT 0; #游标的标志位
      DECLARE vproduct int;
      DECLARE vprice int;
      DECLARE vtime date;  # DECLARE cur1 CURSOR FOR SELECT product,price,`time` FROM pro ;# where price>0 ;
      
      DECLARE cur1 CURSOR FOR SELECT product,price,`time` FROM pro where price>0 order by product ,  `time` desc ,price desc  ;
      DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
      create TEMPORARY table tmp(product int,price int,`time` date);
      OPEN cur1;
      REPEAT
        FETCH cur1 INTO vproduct,vprice,vtime;
        IF NOT done THEN 
          if exists(select 1 from pro where product=vproduct and `time`<vtime and price>vprice) then #   and not exists(select 1 from pro where product=vproduct and `time`>vtime and price<vprice)  then
              if (select count(*) from pro where product=vproduct)=2 then
                    insert into tmp select * from pro where product=vproduct order by price desc;
              else
                  if (select count(*) from tmp where product=vproduct) <2 then
                    insert into tmp values(vproduct,vprice,vtime);
                  end if;
              end if;
           end if;
        END IF;
      UNTIL done END REPEAT;
      CLOSE cur1;
      select * from tmp order by product,price desc limit 20;
      drop table tmp;
    end;
    3:测试结果mysql> call get_price_down();
    +---------+-------+------------+
    | product | price | time       |
    +---------+-------+------------+
    |       1 |    10 | 2010-01-06 |
    |       1 |     8 | 2010-01-07 |
    |       2 |     5 | 2010-01-03 |
    |       2 |     3 | 2010-01-05 |
    |       5 |    10 | 2010-01-03 |
    |       5 |     9 | 2010-01-04 |
    +---------+-------+------------+
    6 rows in set (0.45 sec)Query OK, 0 rows affected (0.50 sec)mysql>