php mysql连接2个没有公共值的表
我有两个选项卡,其中包含以下字段php mysql连接2个没有公共值的表,php,mysql,Php,Mysql,我有两个选项卡,其中包含以下字段 表1: Id, Name, startDate, File, Current test 数据集: 1 nm1-tbl1 25-10-2013 file1 yes 1 1 nm2-tbl1 27-10-2013 file2 yes 1 表2: Id, Name, startDate, File, Enddate 资料 我需要把这本书出版 1 nm1-tbl2 24-10-2013 file1 1 nm1-tbl1 25-10-2013 file
表1
:
Id, Name, startDate, File, Current test
数据集:
1 nm1-tbl1 25-10-2013 file1 yes 1
1 nm2-tbl1 27-10-2013 file2 yes 1
表2
:
Id, Name, startDate, File, Enddate
资料
我需要把这本书出版
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或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
选择a.*和b.*
表1为a,表2为b
其中a.File“”和b.File“”以及a.startDate“0000-00-00”
和b.开始日期“0000-00-00”由a.开始日期ASC、b.开始日期ASC订购
但它并没有像预期的那样发挥作用。它先订购表1,然后订购表2。但我需要2的组合。如何做到这一点。请帮帮我 我认为您需要在这里使用UNION:
(SELECT * FROM Table1)
UNION
(SELECT * FROM Table2)
ORDER BY startDate DESC;
UNION用于将多个SELECT语句的结果合并到单个结果集中。
使用联合查询获得此结果
SELECT Id,Name,startDate,File
FROM table1
UNION
SELECT Id,Name,startDate,File
FROM table2
ORDER BY startDate ASC
这是行不通的,因为两个表的列数不同。
(
select
Id,
Name,
startDate,
File
from
table1
)
union
(
select
Id,
Name,
startDate,
File
from
table2
)
order by
startDate DESC;
SELECT Id,Name,startDate,File
FROM table1
UNION
SELECT Id,Name,startDate,File
FROM table2
ORDER BY startDate ASC