Good day. There are two tables

1 act id | name | data 1 | Ivan | 2015-01-01 2 | Petr | 2015-01-03 2 serv id | name | data 1 | Serge | 2015-01-02 2 | John | 2015-01-04 

How can I make a sample to be distributed according to dates, but from two tables. so that data from data=2015-01-01 , then data=2015-01-02 , then data=2015-01-03 , data=2015-01-04 , and another question - whether the tables should have the same cell names ? If mySQl is not possible, then tell me how you can process this data through php so that it would turn out as I requested.

  • select * from act union select * from serve order by data - splash58

2 answers 2

Should the tables have the same cell names?

not necessary. it is enough to list them in the right order.

eg:

SQL feeddle

MySQL 5.6 Schema Setup :

 create table act (id int, name text, data date); create table serv (name1 text, data1 date, id1 int); insert into act values (1, 'иван', '2015-01-01') ,(2, 'пётр', '2015-01-03') ; insert into serv (id1, name1, data1) values (1, 'сергей', '2015-01-02') ,(2, 'джонни', '2015-01-04') ; 

Query 1 :

 select id, name, data from act union select id1, name1, data1 from serv order by data 

Results :

 | id | name | data | |----|--------|---------------------------| | 1 | иван | January, 01 2015 00:00:00 | | 1 | сергей | January, 02 2015 00:00:00 | | 2 | пётр | January, 03 2015 00:00:00 | | 2 | джонни | January, 04 2015 00:00:00 | 
  • Thank you you gave the most complete answer - Sergalas
 select id, name, data from ( select id, field1 AS name, field2 AS data from act union select id, field3 AS name, field4 AS data from serv ) order by data;