我有一个 php 页面,想显示来自多个表(目前是 3 个表)的数据
所有表格都有日期时间列
和其他列完全不同(数字和数据类型等)
我要展示
三个表中任一表的最新行
三个表中任意一个的第二行
三个表中任一表的第三行
等等
我的表结构如下
ATTANDANCE [a_id,date-time,employee,attandance_more....]
TASKS_ASSIGN[ta_id,date-time,employee,title,detail,duration,task_assign_more.....]
TASK_COMPLETED [tc_id,date-time,employee,stars,task_completed_more....]
我想显示为 facebook
Alam has joined office at 23-mar-2013 **08:00am**
Haider has joined office at 23-mar-2013 **08:01am**
Alam has completed xyz-task at 22-mar-2013 **03:45pm**
Alam has joined office at 22-mar-2013 **10:12am**
......
最佳答案
您似乎只需要列的子集。我不确定表格和结果之间的确切关系是什么,但类似于:
select 'attandance' as which, employee, `datetime`
from attendance
union all
select 'assign' as which, employee, `datetime`
from tasks_assign
union all
select `completed` as which, employee, `datetime`
from tasks_completed
order by `datetime` desc
也就是说,您可以使用子查询来获取每个表中列的子集。然后使用 union all
将它们组合在一起,并使用 order by
将它们按您想要的顺序排列。
关于php - 按日期时间显示来自多个表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131358/