php mysql加入2个没有常用值的表

时间:2014-09-20 11:13:00

标签: php mysql

我有两个包含以下字段的标签

Table1 ::

Id, Name, startDate, File, Current test

数据集:

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

Table2 ::

Id, Name, startDate, File, Enddate

数据

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

我需要输出

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 

两个表都没有共同的值。但我需要按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

但它没有按预期工作。它首先命令table1然后table2。但我需要结合2.如何实现这一目标。请帮我。

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我认为你需要在这里使用UNION:

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

UNION用于将多个SELECT语句的结果合并到一个结果集中。 http://dev.mysql.com/doc/refman/5.0/en/union.html

答案 2 :(得分:0)

使用union query获取此结果

SELECT Id,Name,startDate,File
FROM table1
UNION
SELECT Id,Name,startDate,File
FROM table2
ORDER BY startDate ASC
相关问题