关联查询的问题

abcat 2005-05-12 04:13:00
mysql

有2张表,结构类似,要取2张表里的信息,按f_ctime倒序排,应该怎么写SQL语句?
table1
f_id,f_title,f_link,f_ctime
1,test,http://test.com/1.htm,2005:05:12 11:11:11
2,test,http://test.com/2.htm,2005:05:13 11:11:11
3,test,http://test.com/3.htm,2005:05:14 11:11:11

table2
f_id,f_title,f_link,f_ctime
1,test,http://test.com/1.htm,2005:05:10 11:11:11
2,test,http://test.com/2.htm,2005:05:17 11:11:11
3,test,http://test.com/3.htm,2005:05:18 11:11:11

需要得到这样的查询结果集:
f_id,f_title,f_link,f_ctime
3,test,http://test.com/3.htm,2005:05:18 11:11:11
2,test,http://test.com/2.htm,2005:05:17 11:11:11
3,test,http://test.com/3.htm,2005:05:14 11:11:11
2,test,http://test.com/2.htm,2005:05:13 11:11:11
1,test,http://test.com/1.htm,2005:05:12 11:11:11
1,test,http://test.com/1.htm,2005:05:10 11:11:11

……问题好象比较蠢 请各位XD多多包涵 不好意思 谢谢先 -_*

...全文
53 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcat 2005-05-16
  • 打赏
  • 举报
回复
谢谢 redhatcn

SELECT f_id,f_title,f_link,f_ctime FROM table1
UNION
SELECT f_id,f_title,f_link,f_ctime FROM table2
ORDER BY f_ctime DESC
abcat 2005-05-13
  • 打赏
  • 举报
回复
谢谢楼上的好人,不过这不是我要的效果:(
是要取2张表的所有信息,然后按结果集的f_ctime倒序排,不需要table1.f_id =table2.f_id,也不是按其中某张表排序。
应该怎么写呢
redhatcn 2005-05-13
  • 打赏
  • 举报
回复
(SELECT f_id,f_title,f_link,f_ctime table1)
UNION
(SELECT f_id,f_title,f_link,f_ctime table2)
ORDER BY f_ctime DESC
abcat 2005-05-13
  • 打赏
  • 举报
回复
没人知道吗?还是问题太简单都不屑回答 :(
taoxuwen 2005-05-12
  • 打赏
  • 举报
回复
select * from table1,table2
where table1.f_id =table2.f_id
order by table1.f_ctime desc

56,675

社区成员

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

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