【问题标题】:PHP - MySql: fetch item from three tables and order by timestampPHP - MySql:从三个表中获取项目并按时间戳排序
【发布时间】:2013-06-15 04:36:49
【问题描述】:

我想知道什么是

从三个不同的表中获取不同项目详细信息的最佳方法 并根据主页上显示的时间戳排序

Suppose :
Table 1
+.......+..........+..........+.......+.........+..........+.........+...........+
:postId : sourceId : fee      : pay   : company : elig     : howto   : timestamp :
+.......+..........+..........+.......+.........+..........+.........+...........+

Table 2
+.......+..........+..........+.......+.........+..........+.........+...........+
:postId : sourceId : skill    : title : funct   : location : exper   : timestamp :
+.......+..........+..........+.......+.........+..........+.........+...........+

Table3
+.......+..........+..........+.......+.........+..........+.........+...........+
:postId : sourceId : company  : title : sector  : quali    : state   : timestamp :
+.......+..........+..........+.......+.........+..........+.........+...........+

所有表项之间没有相似性,我不能将它们都放在一个表中。

现在,我想在我的应用程序HOME PAGE according to their timestamp 上显示所有表中的项目。

我知道如何按顺序获取。我正在使用以下方式:

 SELECT * FROM Table1;   //Dispaly all items of table1
and then 
 SELECT * FROM Table2;   //Dispaly all items of table2

Similarlly from table3.

有没有办法从所有三个表中选择全部并按它们的时间戳排序。

我读了一个article,它告诉使用带有(id, timestamp, table_type) 的单独表,并根据时间戳按id 顺序获取项目,但在我的情况下,所有表项目具有不同的顺序(可能是来自不同表的两个项目具有相同ID)。

【问题讨论】:

  • 这3张表之间有关系吗?
  • @YogeshSuthar 不,表之间没有任何关系
  • 然后检索每个查询的结果并将其存储在单个数组中,并在检索结果后根据时间戳对数组进行排序。

标签: php mysql sql database-design timestamp


【解决方案1】:

这个怎么样:

select timestamp, postId, sourceId, fee, pay, company, elig, howto, 
       null as skill, null as title, null as funct, null as location, null as exper, 
       null as company, null as title, null as sector, null as quali, null as state
from   table1
union all
select timestamp, postId, sourceId, null as fee, null as pay, null as company, null as elig, null as howto, 
       skill, title, funct,  location, exper, 
       null as company, null as title, null as sector, null as quali, null as state
from   table2
union all
select timestamp, postId, sourceId, null as fee, null as pay, null as company, null as elig, null as howto, 
       null as skill, null as title, null as funct, null as location, null as exper, 
       company, title, sector, quali, state
from   table3
order by timestamp;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2011-08-22
    • 2021-02-19
    • 2019-08-11
    相关资源
    最近更新 更多