Vasanthan.R.P
Vasanthan.R.P

Reputation: 1287

php mysql Join 2 tables without common values

I have 2 tabes with the following fields

Table1::

Id, Name, startDate, File, Current test

Data set:

1 nm1-tbl1  25-10-2013 file1 yes  1
1 nm2-tbl1  27-10-2013 file2 yes  1

Table2::

Id, Name, startDate, File, Enddate

Data

1 nm1-tbl2  24-10-2013 file1 11-11-2014
1 nm2-tbl3  26-10-2013 file2 11-11-2014

I need the out put as

1 nm1-tbl2  24-10-2013 file1 
1 nm1-tbl1  25-10-2013 file1 
1 nm2-tbl3  26-10-2013 file2 
1 nm2-tbl1  27-10-2013 file2 

Both tables have no common values. But I need to combile these 2 tables in order by the ASC OR DESC

select a.*, b.* 
from table1 as a, table2 as b 
where a.File <> '' AND  b.File <> '' AND a.startDate <> '0000-00-00'  
  AND b.startDate <> '0000-00-00'  order by a.startDate ASC, b.startDate ASC

But it is not working as expected. It first orders the table1 and then table2. But I need as combination of 2. How to achieve this. Please help me.

Upvotes: 1

Views: 548

Answers (3)

Nikul
Nikul

Reputation: 1025

Use union query to get this result

SELECT Id,Name,startDate,File
FROM table1
UNION
SELECT Id,Name,startDate,File
FROM table2
ORDER BY startDate ASC

Upvotes: 0

schlagi123
schlagi123

Reputation: 735

(
  select
    Id,
    Name,
    startDate,
    File
  from
    table1
)
union
(
  select
    Id,
    Name,
    startDate,
    File
  from
    table2
)
order by 
  startDate DESC;

Upvotes: 2

Raimondas Kazlauskas
Raimondas Kazlauskas

Reputation: 249

I think you need to use UNION here:

(SELECT * FROM Table1)
UNION
(SELECT * FROM Table2)
ORDER BY startDate DESC;

UNION is used to combine the result from multiple SELECT statements into a single result set. http://dev.mysql.com/doc/refman/5.0/en/union.html

Upvotes: 0

Related Questions