请教在存储过程里从四张表里查询数据

wangtiantian23 2017-04-05 03:10:14
目前我有四张表,表结构一摸一样(具体历史原因不在深究了)。table1,table2,table3,table4
现在只想查出最四张表里的最后一条记录。

select * from table1 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1;

select * from table2 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1;

select * from table3 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1;

select * from table4 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1;

怎么样写个存储过程,把这四张表查询出来的结果显示在一张表里。

万望各位大神不吝赐教!!!
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
二月十六 2017-04-05
  • 打赏
  • 举报
回复
e.g.
SELECT * FROM (
SELECT * from table1 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) desc limit 0,1
union all
select * from table2 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) desc limit 0,1
union all
select * from table3 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) desc limit 0,1
union all
select * from table4 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) desc limit 0,1)t
order by id 
wangtiantian23 2017-04-05
  • 打赏
  • 举报
回复
@rucypli 非常感谢!搞好了!百度了一下,少了括号。 (select * from table1 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1) union all (select * from table2 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1) union all (select * from table3 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1) union all (select * from table4 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1); 这样就好了!非常感谢!
wangtiantian23 2017-04-05
  • 打赏
  • 举报
回复
@rucypli 报错了! [Err] 1221 - Incorrect usage of UNION and ORDER BY
rucypli 2017-04-05
  • 打赏
  • 举报
回复
select * from table1 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1 union all select * from table2 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1 union all select * from table3 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1 union all select * from table4 where DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= date(yuer_time) order by id desc limit 0,1;
wangtiantian23 2017-04-05
  • 打赏
  • 举报
回复
请问这个有可能吗?在线等~~

56,677

社区成员

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

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