数据库有表如下:
2015-10-1-table/2015-10-2-table……2015-10-31-table
共31个表。每个表结构一样,都有id,都有code,都有money。现在要查出每个表里,每个money的最高值对应的code。
sql怎没写?

解决方案 »

  1.   

    (select '2015-10-1-table' as k,`money`,`code` from `2015-10-1-table` order by `money` desc limit 1)
    union all
    (select '2015-10-2-table' as k,`money`,`code` from `2015-10-2-table` order by `money` desc limit 1)
    union all
    (select '2015-10-3-table' as k,`money`,`code` from `2015-10-3-table` order by `money` desc limit 1)
    union all
    ...
    union all
    (select '2015-10-31-table' as k,`money`,`code` from `2015-10-31-table` order by `money` desc limit 1)
      

  2.   

    SELECT id,code,max(money)
    FROM 2015-10-1-table
    GROUP BY id,code
    UNION ALL
    SELECT id,code,max(money)
    FROM 2015-10-2-table
    GROUP BY id,code
    .....
      

  3.   

    1楼的这个代码可以,我测试过了,顶一下:
            (select '2015-10-1-table' as k,`money`,`code` from `2015-10-1-table` order by `money` desc limit 1)
    union all
    (select '2015-10-2-table' as k,`money`,`code` from `2015-10-2-table` order by `money` desc limit 1)
    union all
    (select '2015-10-3-table' as k,`money`,`code` from `2015-10-3-table` order by `money` desc limit 1)
    union all
    ...
    union all
    (select '2015-10-31-table' as k,`money`,`code` from `2015-10-31-table` order by `money` desc limit 1)
      

  4.   

    union all关联 所有表