如下逻辑,sql怎么写?

knxw0001 2015-11-02 06:20:09
数据库有表如下:
2015-10-1-table/2015-10-2-table……2015-10-31-table
共31个表。每个表结构一样,都有id,都有code,都有money。

现在要查出每个表里,每个money的最高值对应的code。
sql怎没写?
...全文
138 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
码无边 2015-11-03
  • 打赏
  • 举报
回复
union all关联 所有表
文修 2015-11-03
  • 打赏
  • 举报
回复
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)
Rotel-刘志东 2015-11-02
  • 打赏
  • 举报
回复
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 .....
ACMAIN_CHM 2015-11-02
  • 打赏
  • 举报
回复
(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)

56,940

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧