[英]PHP: union data from 2 access tables with different column names and order
Forgive me for the simplicity of this question but i am pretty new to this. 原谅我这个问题的简单性,但是我对此很陌生。
I have an access file from which till now retrieved data from one table through php and it was working like a charm. 我有一个访问文件,到目前为止,该文件是通过php从一个表中检索数据的,它的工作原理很吸引人。 Now i have 2 tables in this file and i need to combine the results of the 2 tables in one table (not combine horizontally with same column but consider one blank table filled with results from both tables). 现在我在该文件中有2个表,我需要将2个表的结果合并到一个表中(不要与同一列水平合并,而是考虑一个空白表,其中填充了两个表的结果)。 Then i need to order this table. 然后我需要订购这张桌子。 I tried with different syntax but failed. 我尝试使用其他语法,但失败了。
Please give me your lights! 请给我你的灯!
What i tried to do is this: 我试图做的是这样的:
<?php
$sql = "SELECT id, Lastname, Firstname, Email FROM `NEW EMPLOYEES`";
$sql .= " UNION SELECT ID, `Last Name`, `First name`, Email FROM `OLD EMPLOYEES`";
$sql .= " ORDER BY Lastname ASC";
?>
As you can see i have different column names per column and this does not shows results. 如您所见,我每列都有不同的列名,并且不显示结果。
Also, how could i order the results depending on lastname column?In my case from UNION from one table column name is Lastname and from other table same column is Last Name
. 另外,我如何根据姓氏列排序结果?在我的情况下,从UNION中一个表的列名称是Lastname,从另一个表中的相同列是Last Name
。
Thank you in advance 先感谢您
try this: 尝试这个:
<?php
$sql = "SELECT * from (SELECT id, Lastname, Firstname, Email FROM `NEW EMPLOYEES`";
$sql .= " UNION SELECT ID, `Last Name`, `First name`, Email FROM `OLD EMPLOYEES`) as union_table";
$sql .= " ORDER BY union_table.Lastname ASC";
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.