php - How to merge two tables and order them based on the timestamp? -


i have 2 tables shown below

table: behaviour

id - hash - page - timestamp 1 - abc - page 1 - 2016-06-06 13:20 2 - abc - page 2 - 2016-06-06 13:18 

table: behaviour_actions

id - hash - page - action - timestamp 1 - abc - page 1 - form - 2016-06-06 13:19 2 - abc - page 2 - form - 2016-06-06 13:12 

a user (hash) has multiple entries in behaviour , multiple entries on behaviour_actions. trying join 2 tables , order timestamp.

so end result mix of 2 tables, in respect time.

my query below not sort them want, because doesn't put each entry of 2 tables below each other based on time. using php. can it?

select behaviour.page, behaviour.timestamp, behaviour_actions.page, behaviour_actions.timestamp, behaviour_actions.action behaviour join behaviour_actions on behaviour.hash = behaviour_actions.hash behaviour.hash =  'abc' 

enter image description here

something should close asking for.

 select 'ba' src       , ba.page       , ba.timestamp       , ba.action    behaviour_actions ba   ba.hash = 'abc'    union   select 'b' src       , b.page       , b.timestamp       , null action    behaviour b   b.hash = 'abc'   order 2 

note 2 queries "line up" union operation, have return same number of columns. return null value placeholder action column in result. return empty string, '' action or other expression. need return fourth column, both queries return 4 columns.

if want row behaviour listed first, if timestamp values match...

  order 2,1 

if want row behaviour first,

  order 1,2 

it's not mandatory return first column, discriminator column. typically when resultset this, want know select returned row. in example, value b tells me query of behaviour returned row. value of ba tells me query of behaviour_actions returned row.