简体   繁体   English

PHP:从2个具有不同列名和顺序的访问表中合并数据

[英]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.

相关问题 SQL Server是否可以使用PHP从UNION查询返回不同的别名列名? - Is it possible for SQL Server to return different aliased column names from a UNION query using PHP? 使用union all和单个命令通过,php从多个表中选择 - Select from multiple tables using union all and single order by ,php 如何从php / mysql中的不同表中获取特定的列数据 - How to fetch specific column data from the different tables in php/mysql 来自具有相同列名的两个表的数据 - Data from two tables with same column names SELECT 来自两个表 WHERE 每个表中的不同列等于 $id ORDER BY 公共列(PHP / MySQL) - SELECT from two tables WHERE different columns in each table equal $id ORDER BY common column (PHP/MySQL) PHP MySQL从2个不同的表中选择*,并按两个表中的datetime列显示混合在一起的数据 - PHP MySQL Select * from 2 different tables and display the data mixed together ordered by datetime column in both tables 在PHP中将2个不同表中的1列匹配在一起 - matching 1 column from 2 different tables together in PHP 将多个表与不同列合并以按日期排序 - Union multiple table with different column for order by date 如果列名与两个表不同,如何添加模型关系? - how to add model relationship if column names are different from two tables? 如何显示来自具有相同列名的两个不同表的结果 - How to display results from two different tables with the same column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM