本帖最后由 joeyhacker 于 2010-06-18 16:59:18 编辑

解决方案 »

  1.   

    作用就是统计某种类型的某个值count的值, 在一个结果集中显示
      

  2.   

    select A.number,A.count,B.number,B.count,C.number,C.count
    from tb A,tb B,tb C
    where A.id+1=B.id and B.id+1=C.id and A.id%3=0 and B.id%3=1 and C.id%3=2
    order by A.count
      

  3.   

    SELECT MAX(IF(TYPE='t1',number,'')) AS t1_number,MAX(IF(TYPE='t1',COUNT,'')) AS t1_count,
    MAX(IF(TYPE='t2',number,'')) AS t2_number,MAX(IF(TYPE='t2',COUNT,'')) AS t2_count,
    MAX(IF(TYPE='t3',number,'')) AS t3_number,MAX(IF(TYPE='t3',COUNT,'')) AS t3_count
    FROM (
    SELECT a.count,a.id,a.number,a.type,COUNT(b.id) AS gs FROM tth5 a LEFT JOIN tth5 b ON a.type=b.type AND a.count<=b.count
    GROUP BY a.count,a.id,a.number,a.type
    ) aa GROUP BY gs
    222应该为T2吧
      

  4.   


    mysql> SELECT MAX(IF(TYPE='t1',number,'')) AS t1_number,MAX(IF(TYPE='t1',COUNT,'
    ')) AS t1_count,
        -> MAX(IF(TYPE='t2',number,'')) AS t2_number,MAX(IF(TYPE='t2',COUNT,'')) AS
    t2_count,
        -> MAX(IF(TYPE='t3',number,'')) AS t3_number,MAX(IF(TYPE='t3',COUNT,'')) AS
    t3_count
        -> FROM (
        -> SELECT a.count,a.id,a.number,a.type,COUNT(b.id) AS gs FROM tth5 a LEFT JO
    IN tth5 b ON a.type=b.type AND a.count<=b.count
        -> GROUP BY a.count,a.id,a.number,a.type
        -> ) aa GROUP BY gs
        -> ;
    +-----------+----------+-----------+----------+-----------+----------+
    | t1_number | t1_count | t2_number | t2_count | t3_number | t3_count |
    +-----------+----------+-----------+----------+-----------+----------+
    | 123       | 10       | 12        | 9        | 666       | 22       |
    | 111       | 5        | 123       | 8        | 888       | 7        |
    | 444       | 3        | 214       | 2        |           |          |
    |           |          | 222       | 1        |           |          |
    +-----------+----------+-----------+----------+-----------+----------+
    4 rows in set (0.01 sec)mysql>
      

  5.   

    如果只有这几个数还好,但如果数据多,用SQL实现显示效率不合理。建议用存储过程和程序来实现这种排版显示上的要求。
      

  6.   

    如果TYPE较多,则用SP来生成
    MAX(IF(TYPE='t1',number,'')) AS t1_number,MAX(IF(TYPE='t1',COUNT,'
    ')) AS t1_count,
    中的T1部份