我有一个php页面,想要显示多个表格中的数据(目前有3个表格)
所有表格都有日期时间列
和其他列完全不同(数字和数据类型等)
我想要显示
最近一行来自三个表中的任何一个
三张桌子中任意一张最近的第二行
三张桌子中任意一张最近的第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....]
我希望将其显示为脸谱
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**
......
答案 0 :(得分:3)
您似乎只想要列的一部分。我不确定表和结果之间的确切关系,但是类似于:
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
将它们按照您想要的顺序排列。
答案 1 :(得分:0)
首先,将所有结果(来自3个表格)添加到一个数组中。($ theArray)
然后使用usort对数组进行排序。像这样:
function cmp($a, $b)
{
if ($a->date-time == $b->date-time) {
return 0;
}
return ($a->date-time < $b->date-time) ? -1 : 1;
}
usort($theArray, "cmp");